在我的数据库模型中,我向表中添加了第二列,该表之前已经有一个链接到同一个表。 我的更改之前的模型如下所示:
[Table("RepairProcesses", Schema = "Data")]
public class RepairProcess : DatabaseBase
{
[DataMember]
[Column("SerialNumberID")]
public Guid SerialNumberID { get; set; }
[ForeignKey("SerialNumberID")]
public virtual SerialNumber SerialNumber { get; set; }
}
现在我将第二个链接添加到表中,模型现在看起来如下:
[Table("RepairProcesses", Schema = "Data")]
public class RepairProcess : DatabaseBase
{
[DataMember]
[Column("SerialNumberID")]
public Guid SerialNumberID { get; set; }
[DataMember]
[Column("ReplacementSerialNumberID")]
public Guid? ReplacementSerialNumberID { get; set; }
[ForeignKey("SerialNumberID")]
public virtual SerialNumber SerialNumber { get; set; }
[ForeignKey("ReplacementSerialNumberID")]
public virtual SerialNumber ReplacementSerialNumber { get; set; }
}
当我现在尝试创建迁移脚本时,EntityFramework会删除foreingn键并创建一个名为SerialNumber_ID的新列。
public override void Up()
{
DropForeignKey("Data.RepairProcesses", "SerialNumberID", "Data.SerialNumbers");
AddColumn("Data.RepairProcesses", "SerialNumber_ID", c => c.Guid());
AddColumn("Data.RepairProcesses", "ReplacementSerialNumberID", c => c.Guid());
CreateIndex("Data.RepairProcesses", "SerialNumber_ID");
CreateIndex("Data.RepairProcesses", "ReplacementSerialNumberID");
AddForeignKey("Data.RepairProcesses", "ReplacementSerialNumberID", "Data.SerialNumbers", "ID");
AddForeignKey("Data.RepairProcesses", "SerialNumber_ID", "Data.SerialNumbers", "ID");
}
public override void Down()
{
DropForeignKey("Data.RepairProcesses", "SerialNumber_ID", "Data.SerialNumbers");
DropForeignKey("Data.RepairProcesses", "ReplacementSerialNumberID", "Data.SerialNumbers");
DropIndex("Data.RepairProcesses", new[] { "ReplacementSerialNumberID" });
DropIndex("Data.RepairProcesses", new[] { "SerialNumber_ID" });
DropColumn("Data.RepairProcesses", "ReplacementSerialNumberID");
DropColumn("Data.RepairProcesses", "SerialNumber_ID");
AddForeignKey("Data.RepairProcesses", "SerialNumberID", "Data.SerialNumbers", "ID");
}
问题是这是一个高效的数据库,当EF为SerialNumber创建一个新的字段时,我丢失了指向正确字段的链接。
当我只删除创建新列的向上/向下脚本中的行时,我遇到了问题,因为EF期望名为SerialNumber_ID的列。
感谢您的帮助, 迈克尔
答案 0 :(得分:1)
尝试以这种方式更改SerialNumber
课程:
public class SerialNumber
{
//other stuff....
[InverseProperty("SerialNumber")]
public virtual ICollection<RepairProcess> SerialNumbers {get;set;}
[InverseProperty("ReplacementSerialNumber")]
public virtual ICollection<RepairProcess> ReplacementSerialNumbers {get;set;}
}