Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

Created Issue: Wrong migration when adding explicit foreign key property to a class [679]

$
0
0
given the class:

public class Product
{
public int Id { get; set; }
virtual PropertiesTemplate PropertiesTemplate { get; set; }
}
when I added explicit foreign key property
public int PropertiesTemplateId { get; set; }
I expected migrations to mark that field in database as not nullable. Instead of that migrations produced code that only renames the column:
public override void Up()
{
DropForeignKey("dbo.Products", "PropertiesTemplate_Id", "dbo.PropertiesTemplates");
DropIndex("dbo.Products", new[] { "PropertiesTemplate_Id" });
RenameColumn(table: "dbo.Products", name: "PropertiesTemplate_Id", newName: "PropertiesTemplateId");
AddForeignKey("dbo.Products", "PropertiesTemplateId", "dbo.PropertiesTemplates", "Id", cascadeDelete: true);
CreateIndex("dbo.Products", "PropertiesTemplateId");
}

if instead of adding int property I tag PropertiesTemplate with [Required] attribute, migrations does the right thing and marks it as non nullable
public override void Up()
{
DropForeignKey("dbo.Products", "PropertiesTemplate_Id", "dbo.PropertiesTemplates");
DropIndex("dbo.Products", new[] { "PropertiesTemplate_Id" });
AlterColumn("dbo.Products", "PropertiesTemplate_Id", c => c.Int(nullable: false));
AddForeignKey("dbo.Products", "PropertiesTemplate_Id", "dbo.PropertiesTemplates", "Id", cascadeDelete: true);
CreateIndex("dbo.Products", "PropertiesTemplate_Id");
}



Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>