Show / Hide Table of Contents

    DefaultValueAttribute Class

    .NET Standard 2.x

    This page focuses on how to use the DefaultValue attribute in SnapObjects. Please refer to .NET API document for more information about System.ComponentModel.DefaultValue.

    The DefaultValue attribute specifies the default value for a property in the model.

    If DefaultValueAttribute is applied to a property in the model class, and the model object has been used for an insert operation, it will automatically set the default value to the corresponding property, on which you have never set a value.

    Remarks

    Notice that it doesn't set the default value (specified by the DefaultValueAttribute) to corresponding property when a new model object is initiated.

    Examples

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

    When a new record is inserted, it will take the default value specified by the DefaultValue attribute for the model object if you have not set any value for this property.

    Model: CourseDescriptionDefaultValue
    using System;
    using System.ComponentModel;
    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.
        /// 
        /// [DefaultValue("Default description")]
        /// 
        /// Specifies that when inserting a new record to database, it will set the 
        /// specified default value to the model object internally if you have never 
        /// set any value for this property. 
        /// </summary>
        [Table("CourseDescription", Schema = "dbo")]
        public class CourseDescriptionDefaultValue
        {
            [Key]
            public int CourseID { get; set; }
    
            [DefaultValue("Default description")]
            public string Description { get; set; }
    
            public DateTime ModifiedDate { get; set; }
    
            public Guid rowguid { get; set; }
        }
    }
    
    Example Method:
    using Appeon.ApiDoc.Models.School;
    using System;
    
    namespace Appeon.ApiDoc.DefaultValueAttributeExamples
    {
        public class DefaultValueAttributeExample
        {
            private SchoolContext _context;
    
            public DefaultValueAttributeExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var mapper = _context.SqlModelMapper;
    
                // Adds a courseDescription record.
                var courseDescription = new CourseDescriptionDefaultValue()
                {
                    CourseID = 1050,
                    ModifiedDate = new DateTime(2018, 01, 01)
                };
    
                Console.WriteLine("Description (before tracked): {0}",
                    courseDescription.Description);
    
                // Inserts the record to database. 
                mapper.TrackCreate(courseDescription);
    
                Console.WriteLine("Description (after tracked): {0}",
                    courseDescription.Description);
    
                mapper.SaveChanges();
    
                /* Internally generated T-SQL:
                
                exec sp_executesql N'INSERT INTO [dbo].[CourseDescription] ([CourseID], 
                [Description], [ModifiedDate], [rowguid])
                VALUES (@pp0, @pp1, @pp2, @pp3);
                SELECT @@ROWCOUNT;
    
                ',N'@pp0 int,@pp1 nvarchar(4000),@pp2 datetime2(7),@pp3 uniqueidentifier',
                @pp0=1050,@pp1=N'Default description',@pp2='2018-01-01 00:00:00',
                @pp3='00000000-0000-0000-0000-000000000000'     
                */
    
    
                /* This code example produces the following output:
                
                Description (before tracked):
                Description (after tracked): Default description
                */
            }
        }
    }
    

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon