[英]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.