adding a foreign key with Code First migration

前端 未结 6 1811
花落未央
花落未央 2020-12-24 02:15

I have an error when trying to update my database after adding a migration.

Here are my classes before add-migration

public class Product
{
    publi         


        
6条回答
  •  滥情空心
    2020-12-24 03:12

    The key to solving this problem is to break your migration into two migrations. First, add a nullable field and fill in the data. Second, make the field a required foreign key.

    First Migration

    1. Add the new property to your class as a nullable type (e.g. int?)

      public class MyOtherEntity
      {
          public int Id { get; set; }
      }
      
      public class MyEntity
      {
          ...
          // New reference to MyOtherEntity
          public int? MyOtherEntityId { get; set; }
          ...
      }
      
    2. Create a migration. NOTE: Migration name is not important, but something like "AddBlogPosts1" makes it easy to read.

      > add-migration AddMyEntityMyOtherEntity1
      
    3. This should scaffold a migration that looks like this:

      public partial class AddMyTableNewProperty1 : DbMigration
      {
          public override void Up()
          {
              AddColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
          }
          public override void Down()
          {
              DropColumn("dbo.MyEntity", "MyOtherEntityId");
          }
      }
      
    4. Now manually edit the generated migration to add a default value for the new field. The easiest case is when the default value is invariant. You can add more logic in the SQL if needed. This example assumed all the MyEntity instances point to the same MyOtherEntity instance with ID 1.

      public partial class AddMyTableNewProperty1 : DbMigration
      {
          public override void Up()
          {
              AddColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
      
              // ADD THIS BY HAND
              Sql(@"UPDATE dbo.MyEntity SET MyOtherEntityId = 1
                    where MyOtherEntity IS NULL");
          }
          public override void Down()
          {
              DropColumn("dbo.MyEntity", "MyOtherEntityId");
          }
      }
      
    5. Update the database

      > update-database
      

    Second Migration

    1. Go back to your MyEntity class and change the new property to represent a mandatory foreign key.

      public class MyEntity
      {
          ...
          // Change the int? to int to make it mandatory
          public int MyOtherEntityId { get; set; }
      
          // Create a reference to the other entity
          public virtual MyOtherEntity MyOtherEntity { get; set; }
          ...
      }
      
    2. Create another migration

      > add-migration AddMyEntityMyOtherEntity2
      
    3. This should create a migration like the following:

      public partial class AddMyEntityMyOtherEntity2: DbMigration
      {
          public override void Up()
          {
              AlterColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int(nullable: false));
              CreateIndex("dbo.MyEntity", "MyOtherEntityId");
              AddForeignKey("dbo.MyEntity", "MyOtherEntityId", "dbo.MyOtherEntity", "Id");
          }
          public override void Down()
          {
              DropForeignKey("dbo.MyEntity", "MyOtherEntityId", "dbo.MyOtherEntity");
              DropIndex("dbo.MyEntity", new[] { "MyOtherEntityId" });
              AlterColumn("dbo.MyEntity", "MyOtherEntityId", c => c.Int());
          }
      }
      
    4. Update the database

      > update-database
      

    Other Notes

    1. This technique works for migrations applied during application start up.
    2. Adding more complex mappings for the new column in the SQL are possible, but not illustrated here.

提交回复
热议问题