Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value for Required fields in Entity Framework migrations?

I've added the [Required] data annotation to one of my models in an ASP.NET MVC application. After creating a migration, running the Update-Database command results in the following error:

Cannot insert the value NULL into column 'Director', table 'MOVIES_cf7bad808fa94f89afa2e5dae1161e78.dbo.Movies'; column does not allow nulls. UPDATE fails. The statement has been terminated.

This is due to some records having NULL in their Director columns. How can I automatically change those values to some default (say "John Doe") director?

Here is my model:

  public class Movie     {         public int ID { get; set; }         [Required]         public string Title { get; set; }          [DataType(DataType.Date)]         public DateTime ReleaseDate { get; set; }          [Required]         public string Genre { get; set; }          [Range(1,100)]         [DataType(DataType.Currency)]         public decimal Price { get; set; }          [StringLength(5)]         public string Rating { get; set; }          [Required]     /// <--- NEW         public string Director { get; set; }     } 

and here is my latest migration:

public partial class AddDataAnnotationsMig : DbMigration {     public override void Up()     {         AlterColumn("dbo.Movies", "Title", c => c.String(nullable: false));         AlterColumn("dbo.Movies", "Genre", c => c.String(nullable: false));         AlterColumn("dbo.Movies", "Rating", c => c.String(maxLength: 5));         AlterColumn("dbo.Movies", "Director", c => c.String(nullable: false));     }      public override void Down()     {         AlterColumn("dbo.Movies", "Director", c => c.String());         AlterColumn("dbo.Movies", "Rating", c => c.String());         AlterColumn("dbo.Movies", "Genre", c => c.String());         AlterColumn("dbo.Movies", "Title", c => c.String());     } } 
like image 994
Andriy Drozdyuk Avatar asked Sep 17 '12 15:09

Andriy Drozdyuk


People also ask

How do I set the default value in Entity Framework?

In the DbContext OnModelCreating you add the default value. The 'bool' property 'Active' on entity type 'Foundation' is configured with a database-generated default. This default will always be used for inserts when the property has the value 'false', since this is the CLR default for the 'bool' type.

How does migration work in Entity Framework?

The migrations feature in EF Core provides a way to incrementally update the database schema to keep it in sync with the application's data model while preserving existing data in the database.


2 Answers

In addition to the answer from @webdeveloper and @Pushpendra, you need to manually add updates to your migration to update existing rows. For example:

public override void Up() {     Sql("UPDATE [dbo].[Movies] SET Title = 'No Title' WHERE Title IS NULL");     AlterColumn("dbo.Movies", "Title", c => c.String(nullable: false,defaultValue:"MyTitle")); } 

This is because AlterColumn produces DDL to set the default of the column to some specific value in the table specification. The DDL does not affect existing rows in the database.

You're actually making two changes at the same time (setting the default and making the column NOT NULL) and each of them is valid individually, but since you're making the two at the same time, you can expect the system to 'intelligently' realize your intent and set all NULL values to the default value, but this is not what's expected all the time.

Suppose you're only setting the default value for the column, and not making it NOT NULL. You obviously don't expect all the NULL records to be updated with the default you provide.

So, in my opinion, this is not a bug, and I don't want EF to update my data in the ways that I don't explicitly tell it to do. The developer is responsible to instruct the system about what to do with the data.

like image 77
Iravanchi Avatar answered Oct 14 '22 11:10

Iravanchi


If I remember correctly, something like this should work:

AlterColumn("dbo.Movies", "Director", c => c.String(nullable: false, defaultValueSql: "'John Doe'")); 

Note: The defaultValueSql parameter value is treated as a verbatim SQL statement, so if the required value is effectively a string, like the John Doe example, then single quotes are required around the value.

like image 41
webdeveloper Avatar answered Oct 14 '22 10:10

webdeveloper