Show / Hide Table of Contents

    SqlDefaultValueAttribute Class

    .NET Standard 2.x

    Specifies that the database will generate a default value for the mapped column.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Inheritance Constructor

    System.Attribute

    Syntax

       [AttributeUsage(AttributeTargets.Property)]
       public class SqlDefaultValueAttribute : Attribute, ISqlDefaultValue
    

    Constructors

    Name Description
    SqlDefaultValueAttribute(string defaultValue) Initializes a new instance of the SqlDefaultValueAttribute class.

    Properties

    Name Return Type Description
    DefaultValue System.String Gets the definition of the default value for the mapped column in database.

    Remarks

    If SqlDefaultValueAttribute is applied to a property in the model class, and the model object has been used for inserting a new record to database, the mapped column will not be saved to the database and the database will generate the default value. After the record is added to the database, the value of the column generated by the database will be returned to the model object.

    Examples

    The following code example demonstrates how to use the SqlDefaultValue attribute.

    When the rowguid property in the CourseDescriptionPropertySave class is applied with the SqlDefaultValue attribute, it a new record is inserted, the database will generate the default value using newid() and the value of the column generated by the database will be returned.

    Model: CourseDescriptionSqlDefaultValue
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using SnapObjects.Data;
    
    namespace Appeon.ApiDoc.Models.School
    {
        /// <summary>
        /// This model class maps to the dbo.CourseDescription table.
        /// 
        /// [SqlDefaultValue("newid()")]
        /// 
        /// Specifies that database will generate default value for the mapped column, 
        /// and the function used be database is "newid()".
        /// </summary>
        [Table("CourseDescription", Schema = "dbo")]
        public class CourseDescriptionSqlDefaultValue
        {
            [Key]
            public int CourseID { get; set; }
            
            public string Description { get; set; }
            
            public DateTime ModifiedDate { get; set; }
    
            [SqlDefaultValue("newid()")]
            public Guid rowguid { get; set; }
    
        }
    }
    
    Example Method:
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.SqlDefaultValueAttributeExamples
    {
        public class SqlDefaultValueAttributeExample
        {
            private SchoolContext _context;
    
            public SqlDefaultValueAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Adds a courseDescription record.
                var courseDescription = new CourseDescriptionSqlDefaultValue()
                {
                    CourseID = 1050,
                    Description = "Calculus Description New",
                    ModifiedDate = new DateTime(2018, 01, 01)
                };
    
                // Inserts a record to the database. 
                mapper.TrackCreate(courseDescription).SaveChanges();
    
                /* Internally generated T-SQL:
                
                exec sp_executesql N'INSERT INTO [dbo].[CourseDescription] ([CourseID], 
                [Description], [ModifiedDate])
                VALUES (@pp0, @pp1, @pp2);
                SELECT [rowguid]
                FROM [dbo].[CourseDescription]
                WHERE @@ROWCOUNT = 1 AND [CourseID] = @pp0;
    
                ',N'@pp0 int,@pp1 nvarchar(4000),@pp2 datetime2(7)',@pp0=1050,
                @pp1=N'Calculus Description New',@pp2='2018-01-01 00:00:00'      
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon