C# 引入外键约束';列';在桌上';型号';可能导致循环或多个级联路径
我想建立关系,但我遇到以下错误: 引入外键约束 “FK_dbo.MatchModels_dbo.lookingforhomes_PropertyID”在表上 “匹配模型”可能导致循环或多个级联路径。指明 删除无操作或更新无操作,或修改其他外键 限制。无法创建约束或索引。见前文 错误 我有两个模型要添加属性,然后除以“PropertyType” 比如,;matchModel有2行 匹配ID 1属性ID 23属性类型2 匹配ID 2属性ID 23属性类型1 如果PropertyType的值为2,则表示LookingForHome或值为1,表示LookingForMate。 这是真的吗?我不知道,但根据这一逻辑,我应该为他们建立关系 MatchModel:PropertyID->LookingForMate:PropertyID 匹配模型:PropertyID->查找主页:PropertyID 我怎样才能建立关系C# 引入外键约束';列';在桌上';型号';可能导致循环或多个级联路径,c#,asp.net-mvc,entity-relationship,C#,Asp.net Mvc,Entity Relationship,我想建立关系,但我遇到以下错误: 引入外键约束 “FK_dbo.MatchModels_dbo.lookingforhomes_PropertyID”在表上 “匹配模型”可能导致循环或多个级联路径。指明 删除无操作或更新无操作,或修改其他外键 限制。无法创建约束或索引。见前文 错误 我有两个模型要添加属性,然后除以“PropertyType” 比如,;matchModel有2行 匹配ID 1属性ID 23属性类型2 匹配ID 2属性ID 23属性类型1 如果PropertyType的值为2,则表
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;
}
}
公共类查找主页
{
[关键]
公共int属性ID{get;set;}
...
公共日期时间插入日期{get;set;}
[必需]
public int UserID{get;set;}
公共列表匹配{get;set;}
公共虚拟用户模型用户{get;set;}
公共虚拟城市模型城市{get;set;}
//默认值
公众寻家
{
InsertionDate=DateTime.Now;
}
}
公共类lookingforamate模型
{
[数据库生成(DatabaseGeneratedOption.Identity)]
[关键]
公共int属性ID{get;set;}
....
[必需]
public int UserID{get;set;}
公共日期时间插入日期{get;set;}
public LookingForaMateModel()
{
InsertionDate=DateTime.Now;
}
公共列表属性照片{get;set;}
公共列表匹配{get;set;}
公共虚拟用户模型用户{get;set;}
公共虚拟城市模型城市{get;set;}
}
公共类匹配模型
{
[关键]
公共int MatchID{get;set;}
[必需]
公共int属性ID{get;set;}
[必需]
公共int属性类型{get;set;}
[必需]
public int UserID{get;set;}
[必需]
公共布尔值为{get;set;}
[必需]
公共日期时间插入日期{get;set;}
公共虚拟用户模型用户{get;set;}
公共虚拟LookingForaMateModel LFMate{get;set;}
公共虚拟查找主LFHome{get;set;}
公共匹配模型(){
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; }