Default value for Required fields in Entity Framework migrations?

asp.net-mvc c# ef-code-first entity-framework

Question

I've inserted the[Required] An MVC application for ASP.NET data annotation for one of my models Developing a migration, executing theUpdate-Database command yields 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.

Due to certain records' NULL values in theirDirector columns. How can I set those settings to a default director, such as "John Doe"?

This is my example:

  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; }
    }

Here is my most recent 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());
    }
}
1
89
2/26/2018 5:23:46 AM

Accepted Answer

If I recall properly, anything along the lines of:

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

Single quotes are necessary around the value if it is practically a string, as in the John Doe example, since the defaultValueSql parameter value is interpreted as a literal SQL query.

74
4/9/2020 10:38:15 AM

Popular Answer

You must manually add changes to your migration in addition to following the advice from @webdeveloper and @Pushpendra if you want to update already-existing rows. For instance:

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 due toAlterColumn creates DDL to set the column's default value in the table specification to a certain value. Existing records in the database are not impacted by the DDL.

Setting the default and making the column NOT NULL are essentially two changes you're making at once, and because you're doing them both at once, you can anticipate the system to "intelligently" understand your purpose and set all of them to what you want.NULL values to the default value, albeit this is not always what is intended.

Assume you're merely changing the column's default value and not making it NOT NULL. Of course, you don't anticipate updating all NULL entries with the default you provide.

Therefore, in my view, this is not a problem, and I do not want EF to change my data in ways that I have not specifically instructed it to. It is the developer's responsibility to provide the system instructions on how to handle the data.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow