Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. But.. This column will NOT be nullable, so I need to provide a default value of true to all the rows that currently exist.

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

IMPORTANT:

(This was in the OP, but people seemed to miss is.)

When databases are updated with the migration, all existing rows which receive this new column MUST have their values set to True.

like image 509
Casey Crookston Avatar asked Dec 02 '16 16:12

Casey Crookston


People also ask

How do you set a Boolean to default?

Set a default valueRight-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value.

What is the default value for a Boolean?

The default value of Boolean is False . Boolean values are not stored as numbers, and the stored values are not intended to be equivalent to numbers. You should never write code that relies on equivalent numeric values for True and False .

How do you set a Boolean default true in Java?

There is no default for Boolean . Boolean must be constructed with a boolean or a String . If the object is unintialized, it would point to null . The default value of primitive boolean is false .


2 Answers

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)); 
like image 184
octavioccl Avatar answered Sep 27 '22 21:09

octavioccl


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

like image 22
Brandon Minnick Avatar answered Sep 27 '22 20:09

Brandon Minnick