Entity framework 5 EF迁移:ALTER TABLE语句与外键约束冲突

Entity framework 5 EF迁移:ALTER TABLE语句与外键约束冲突,entity-framework-5,Entity Framework 5,我上过这些课 public class Bid : ... { ... [Required] public virtual TraderUser Trader { get; set; } } public class TraderUser : ... { ... } 然后,我用以下方式更改了这些类,并添加了一个新类 public class Bid : ... { ... [Required] public virtual TraderUser

我上过这些课

public class Bid : ...
{
   ...

   [Required]
   public virtual TraderUser Trader { get; set; }
}

public class TraderUser : ...
{
   ...
}
然后,我用以下方式更改了这些类,并添加了一个新类

public class Bid : ...
{
   ...

   [Required]
   public virtual TraderUser TraderUser { get; set; }
}

public class TraderUser : ...
{
   ...

   public int TraderCompanyId { get; set; }

   [ForeignKey("TraderCompanyId")]
   public virtual TraderCompany TraderCompany { get; set; }
}

public class TraderCompany : ...
{
   ...
}
当我更新数据库时,我得到了以下错误

ALTER TABLE语句与外键约束冲突 “FK_dbo.Bid_dbo.TraderUser_TraderUser_Id”。冲突发生在 数据库“LeasePlan.Development”,表“dbo.TraderUser”,列“Id”


我无法更新数据库。非常感谢您的帮助。

我不知道是否太迟了,但我也遇到了同样的问题,也许这可以帮助您

我无法从您的帖子中看到,但可能您的TraderUser表已经插入了一些行。您试图完成的是创建新表TraderCompany,并在TraderRuser中创建指向TraderCompany表的外键关系

在一次迁移中,您试图为已经包含数据的表创建不可为空的外键关系

您可以尝试执行以下操作:

  • 第一次迁移-除了这一行,所有内容都相同

    public int TraderCompanyId { get; set; } 
    
    应该是

    public int? TraderCompanyId { get; set; }
    
    这将允许您创建可为空的外键列

  • 使用TraderCompany表中的某些行更新现有数据的TraderCompanyId列

  • 第二次迁移-更改源代码

    public int? TraderCompanyId { get; set; }
    

    并运行您的迁移


我希望这将对您有所帮助。

另一种方法是在迁移代码中添加SQL语句,以便在添加外键之前插入一行。下面是我所做工作的一个例子:

        // Countries is a new table
        CreateTable(
            "dbo.Countries",
            c => new
                {
                    CountryID = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Currency = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.CountryID);
        // Heres where i insert a row into countries
        Sql("INSERT INTO Countries (Name, Currency) VALUES ('United Kingdom', 0)");
        // I set the default value to 1 on the ID fields
        AddColumn("dbo.Brokers", "CountryID", c => c.Int(nullable: false, defaultValue: 1));
        AddColumn("dbo.Products", "CountryID", c => c.Int(nullable: false, defaultValue: 1));
        AddForeignKey("dbo.Brokers", "CountryID", "dbo.Countries", "CountryID", cascadeDelete: false);
        AddForeignKey("dbo.Products", "CountryID", "dbo.Countries", "CountryID", cascadeDelete: false);
        // Migrations then creates index's
        CreateIndex("dbo.Brokers", "CountryID");
        CreateIndex("dbo.Products", "CountryID");

你想在这三门课之间建立什么样的关系?在
TraderCompany
类中是否有任何属性?出价有一个交易者用户,交易者用户有一个交易者公司。TraderCompany只有几个字符串属性。非常感谢您提供此解决方案,我从两天以来一直在努力解决同一问题。非常感谢!!救了我一个早上
        // Countries is a new table
        CreateTable(
            "dbo.Countries",
            c => new
                {
                    CountryID = c.Int(nullable: false, identity: true),
                    Name = c.String(),
                    Currency = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.CountryID);
        // Heres where i insert a row into countries
        Sql("INSERT INTO Countries (Name, Currency) VALUES ('United Kingdom', 0)");
        // I set the default value to 1 on the ID fields
        AddColumn("dbo.Brokers", "CountryID", c => c.Int(nullable: false, defaultValue: 1));
        AddColumn("dbo.Products", "CountryID", c => c.Int(nullable: false, defaultValue: 1));
        AddForeignKey("dbo.Brokers", "CountryID", "dbo.Countries", "CountryID", cascadeDelete: false);
        AddForeignKey("dbo.Products", "CountryID", "dbo.Countries", "CountryID", cascadeDelete: false);
        // Migrations then creates index's
        CreateIndex("dbo.Brokers", "CountryID");
        CreateIndex("dbo.Products", "CountryID");