我有很多课程,但在 PorductionLine
和 Machine
我有一些问题 . ProductionLine类是:
[Column("FldKeyId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
[Key]
public int MyKeyId { get; set; }
[Column("FldCode")]
[Required]
[Index(IsUnique = true)]
public int MyCode
{
get { return _Code; }
set { _Code = value; }
}
[Column("FldName")]
[Required]
public string MyName
{
get { return _Name; }
set { _Name = value; }
}
[Column("FldLocation")]
[Required]
public string MyLocation
{
get { return _Location; }
set { _Location = value; }
}
[Column("FldCompanyKey")]
public int MyCompanyKey { get; set; }
[ForeignKey("MyCompanyKey")]
[Required]
public virtual Company Company { get; set; }
和机器类是:
[Column("FldKeyId")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Required]
[Key]
public int MyKeyId { get; set; }
[Column("FldCode")]
[Required]
[Index(IsUnique = true)]
public int MyMachineCode
{
get { return _MachineCode; }
set { _MachineCode = value; }
}
[Column("FldName")]
[Required]
public string MyName
{
get { return _Name; }
set { _Name = value; }
}
[Column("FldProductionLineKey")]
public int MyProductionLineKey { get; set; }
[ForeignKey("MyProductionLineKey")]
//[Required]
public ProductionLine ProductionLine { get; set; }
当我想生成这个类的数据库时我有这个错误:
在表'TblMachine'上引入FOREIGN KEY约束'FK_dbo.TblMachine_dbo.TblProductionLine_FldProductionLineKey'可能会导致循环或多个级联路径 . 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束 .
无法创建约束或索引 . 查看以前的错误 . 当我评论这3行
[Column("FldProductionLineKey")]
public int MyProductionLineKey { get; set; }
[ForeignKey("MyProductionLineKey")]
错误消失了,但我想在其他一些类中使用这些代码cus我有这个问题...我该怎么办?感谢帮助!!
1 回答
您可以将cascadeDelete设置为false或true(在您的迁移Up()方法中) . 取决于您的要求 .
有关更多信息,请查看this question