![](/img/trans.png)
[英]ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths
[英]Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths
我想建立关系,但出现此错误:
在表“ MatchModels”上引入FOREIGN KEY约束“ FK_dbo.MatchModels_dbo.LookingForaHomes_PropertyID”可能会导致循环或多个级联路径。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 无法创建约束或索引。 请参阅先前的错误。
我有两种添加属性的模型,并且按“ PropertyType”进行划分
例如; matchModel有2行;
MatchID 1 PropertyID 23 PropertyType 2 ....
MatchID 2 PropertyID 23 PropertyType 1 ....
如果PropertyType值为2,则它用于LookingForaHome;如果值是1,则用于LookingForaMate。 我不知道这是真的吗,但根据这种逻辑,我应该为
MatchModel:PropertyID-> LookingForaMate:PropertyID
MatchModel:PropertyID-> LookingForaHome:PropertyID
我该如何建立关系?
public class LookingForaHome
{
[Key]
public int PropertyID { get; set; }
...
public DateTime InsertionDate{ get; set; }
[Required]
public int UserID { get; set; }
public List<MatchModel> Match{ get; set; }
public virtual UserModel User { get; set; }
public virtual CityModel City { get; set; }
//Default Values
public LookingForaHome()
{
InsertionDate = DateTime.Now;
}
}
public class LookingForaMateModel
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int PropertyID { get; set; }
....
[Required]
public int UserID { get; set; }
public DateTime InsertionDate { get; set; }
public LookingForaMateModel()
{
InsertionDate = DateTime.Now;
}
public List<PropertyPhotoModel> PropertyPhoto { get; set; }
public List<MatchModel> Match { get; set; }
public virtual UserModel User { get; set; }
public virtual CityModel City { get; set; }
}
public class MatchModel
{
[Key]
public int MatchID { get; set; }
[Required]
public int PropertyID { get; set; }
[Required]
public int PropertyType { get; set; }
[Required]
public int UserID { get; set; }
[Required]
public bool IsSeen { get; set; }
[Required]
public DateTime InsertionDate { get; set; }
public virtual UserModel User { get; set; }
public virtual LookingForaMateModel LFMate { get; set; }
public virtual LookingForaHome LFHome{ get; set; }
public MatchModel() {
InsertionDate = DateTime.Now;
}
}
更简单的更改是为每种类型使用两个可为空的列。
public class MatchModel
{
[Key]
public int MatchID { get; set; }
[ForeignKey("LFMate")]
public int? MatePropertyID { get; set; }
[ForeignKey("LFHome")]
public int? HomePropertyType { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.