How to set a default value on a Boolean in a Code First model?

前端 未结 5 546
孤独总比滥情好
孤独总比滥情好 2020-12-31 00:13

I have an existing table / model into which I want to drop a new Boolean column. This table already has many hundreds of rows of data, and I can\'t touch the existing data.

相关标签:
5条回答
  • 2020-12-31 00:33

    Another option is create a default constructor and set the properties with the default values you need:

    public class Revision
    {
        public Boolean IsReleased { get; set; }
    
        public Revision()
        {
            IsReleased=true;
    
        }
    }
    

    To set the values to true of the existing rows when you run Update-Database command, you could do this in your Configuration class:

    protected override void Seed(YourContext context)
    {
        var entities=context.Revisions.Where(r=>!r.IsReleased)
        foreach(var e in entities)
        {
          e.IsReleased=true;
         //context.Entry(e).State = EntityState.Modified; If you have disabled change tracking then add this line
        }
        context.SaveChanges();
    }
    

    Update

    If it is a new column you are adding via migration maybe you can also do this:

    AddColumn("dbo.Revisions", "IsReleased", c => c.Boolean(nullable: false, defaultValue: true));
    
    0 讨论(0)
  • 2020-12-31 00:35

    You can simply avoid auto-implemented properties and set the property value to true when you initialize your object.

    private Boolean _isReleased = true;
    public Boolean IsReleased 
    { 
        get
        {
            return _isReleased;
        }
        set
        {
            _isReleased = value;
        }
    }
    
    0 讨论(0)
  • 2020-12-31 00:46

    You can avoid using fields and take advantage of Auto-property initialization, a feature new in C# 6.

    This will set the default value to true when the column is added to your database.

    public class Revision
    {
        ...
        public Boolean IsReleased { get; set; } = true;
        ....
    }
    

    Edit to include @BrewMate's comment:

    If all of your values set to false when you update the database, make sure to have the JSON formatter handle default values. The JSON formatter will ignore default values by default and then your database is setting the boolean to its default value, false. See the link below, I would try Default as the enumeration: http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_DefaultValueHandling.htm

    0 讨论(0)
  • 2020-12-31 00:48

    Unfortunately I wasn't on the version of C# to be able to use @Brandon Minnick's suggestion which seems the simplest. Other suggestions just seemed like to much work instead I ended up doing this: https://stackoverflow.com/a/46436861/1819403

    0 讨论(0)
  • 2020-12-31 00:49

    According to the MSDN, DefaultValueAttribute specifies the default value for a property. You can use DefaultValueAttribute as the following:

    public class Revision
    {
        ...
        [DefaultValue(true)]
        public Boolean IsReleased { get; set; } = true;
        ....
    }
    

    Furthermore you can use UP() method inside of DbMigration class as the following:

    public partial class InitializeDb : DbMigration
    {
        public override void Up()
        {
                CreateTable(
                "dbo.Revision",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        ...
                        IsReleased = c.Boolean(nullable: false, defaultValue: true),
                        ...
                    })
                .PrimaryKey(t => t.Id);
        }
    }
    

    You should add "defaultValue: true" by yourself.

    0 讨论(0)
提交回复
热议问题