[英]Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths
I want to make relationship but i getting this error: 我想建立关系,但出现此错误:
Introducing FOREIGN KEY constraint 'FK_dbo.MatchModels_dbo.LookingForaHomes_PropertyID' on table 'MatchModels' may cause cycles or multiple cascade paths.
在表“ MatchModels”上引入FOREIGN KEY约束“ FK_dbo.MatchModels_dbo.LookingForaHomes_PropertyID”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。 Could not create constraint or index.
无法创建约束或索引。 See previous errors.
请参阅先前的错误。
I have two model to add property and I divide by 'PropertyType' 我有两种添加属性的模型,并且按“ PropertyType”进行划分
For example; 例如; matchModel have 2 rows;
matchModel有2行;
MatchID 1 PropertyID 23 PropertyType 2.... MatchID 1 PropertyID 23 PropertyType 2 ....
MatchID 2 PropertyID 23 PropertyType 1.... MatchID 2 PropertyID 23 PropertyType 1 ....
if PropertyType value is 2, its for LookingForaHome or value is 1 it's for LookingForaMate. 如果PropertyType值为2,则它用于LookingForaHome;如果值是1,则用于LookingForaMate。 Is it true way I dont know but according to this logic I should make relationship for
我不知道这是真的吗,但根据这种逻辑,我应该为
MatchModel: PropertyID -> LookingForaMate: PropertyID MatchModel:PropertyID-> LookingForaMate:PropertyID
MatchModel: PropertyID -> LookingForaHome: PropertyID MatchModel:PropertyID-> LookingForaHome:PropertyID
How can i make relationship ? 我该如何建立关系?
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;
}
}
Simpler changes is to use two nullable columns for each type. 更简单的更改是为每种类型使用两个可为空的列。
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.