locked
EF 6 and Default Values RRS feed

  • Question

  • User-1188570427 posted

    Does EF 6 deploy anything for Default Values? I do not see anything for EF 6, but do in EF Core. Of course, the database will handle it in the end.

    When I scaffold EF Core, then I see the below:

                    entity.Property(e => e.FilteredOrganizationId).HasDefaultValueSql("((1))");

    Thursday, June 14, 2018 6:35 PM

Answers

  • User283571144 posted

    Hi tvb2727,

    This is for EF 6 and non-code first. I do not do an UP() method or anything like that.

    According to your description, I guess you may used the model first EF.

    If you want to set the default value, I suggest you could firstly open the EDMX fild and locate the property which you want to set the default value.

    Then you could right click the property and find the default value setting.

    More details, you could refer to below image:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 20, 2018 7:17 AM

All replies

  • User1120430333 posted

    https://msdn.microsoft.com/en-us/library/system.componentmodel.defaultvalueattribute%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Friday, June 15, 2018 12:04 AM
  • User283571144 posted

    Hi tvb2727,

    Does EF 6 deploy anything for Default Values? I do not see anything for EF 6, but do in EF Core. Of course, the database will handle it in the end.

    As far as I know, the EF6 doesn't contain the HasDefaultValueSql method for the property as ef core.

    If you want to modify the default value, I suggest you could try to directly modify the migration file as below:

    namespace WebApplication1.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
        
        public partial class da2 : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.People",
                    c => new
                        {
                            Id = c.Int(nullable: false, identity: true),
                            Age = c.Int(nullable: false,defaultValue:3),
                        })
                    .PrimaryKey(t => t.Id);
                
            }
            
            public override void Down()
            {
                DropTable("dbo.People");
            }
        }
    }
    

    or

    public override void Up()
    {    
       AddColumn("dbo.Events", "Active", c => c.Boolean(nullable: false, defaultValue: true));
    } 

    More details about how to set the defaultvalue in ef6 code first, you could refer to beliow answer:

    https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value 

    Best Regards,

    Brando

    Friday, June 15, 2018 5:18 AM
  • User-1188570427 posted

    Hi tvb2727,

    tvb2727

    Does EF 6 deploy anything for Default Values? I do not see anything for EF 6, but do in EF Core. Of course, the database will handle it in the end.

    As far as I know, the EF6 doesn't contain the HasDefaultValueSql method for the property as ef core.

    If you want to modify the default value, I suggest you could try to directly modify the migration file as below:

    namespace WebApplication1.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
        
        public partial class da2 : DbMigration
        {
            public override void Up()
            {
                CreateTable(
                    "dbo.People",
                    c => new
                        {
                            Id = c.Int(nullable: false, identity: true),
                            Age = c.Int(nullable: false,defaultValue:3),
                        })
                    .PrimaryKey(t => t.Id);
                
            }
            
            public override void Down()
            {
                DropTable("dbo.People");
            }
        }
    }

    or

    public override void Up()
    {    
       AddColumn("dbo.Events", "Active", c => c.Boolean(nullable: false, defaultValue: true));
    } 

    More details about how to set the defaultvalue in ef6 code first, you could refer to beliow answer:

    https://stackoverflow.com/questions/19554050/entity-framework-6-code-first-default-value 

    Best Regards,

    Brando

    This is for EF 6 and non-code first. I do not do an UP() method or anything like that.

    Monday, June 18, 2018 2:27 PM
  • User283571144 posted

    Hi tvb2727,

    This is for EF 6 and non-code first. I do not do an UP() method or anything like that.

    According to your description, I guess you may used the model first EF.

    If you want to set the default value, I suggest you could firstly open the EDMX fild and locate the property which you want to set the default value.

    Then you could right click the property and find the default value setting.

    More details, you could refer to below image:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 20, 2018 7:17 AM