每个练习都有一个与之相关的人。
Each Excercise has a person associated with it.
每个练习都有一系列与之相关的参与。
Each Excercise has a Collection of Engagements associated with it.
每个参与有2人关联它。我需要帮助Rowan。
Each Engagement has 2 people associated with it. I need help Rowan.
当我使用RelatedTo属性修饰SurveyPerson时,我得到了这个错误。
I am gettting this error when I decorate the SurveyPerson with a RelatedTo attribute.
参考关系将导致周期性不允许的引用。 [约束名称= Excercise_SurveyPerson]
The referential relationship will result in a cyclical reference that is not allowed. [ Constraint name = Excercise_SurveyPerson ]
namespaceTPS.KLE.Data{publicclassKLE_DB:DbContext{publicDbSet<Person>People{get;set;}publicDbSet<Excercise>Excercises{get;set;}publicDbSet<Engagement>Engagements{get;set;}} publicclassExcercise{publicintID{get;set;}publicstringName{get;set;}publicintInitalSiteSurveyPesonID{get;set;} publicvirtualICollection<Engagement>Engagements{get;set;} //thisattributecausesthecyclicalreferenceerror//withouttheattribute,SurveyPersonisnull[RelatedTo(ForeignKey="InitalSiteSurveyPesonID")]publicvirtualPersonSurveyPerson{get;set;}}publicclassEngagement{ publicintID{get;set;}publicintExcerciseID{get;set;}publicintOwnerID{get;set;}publicintTargetID{get;set;} publicvirtualExcerciseExcercise{get;set;}publicvirtualPersonOwner{get;set;}publicvirtualPersonTarget{get;set;}}publicclassPerson{publicintID{get;set;}publicstringFname{get;set;}publicstringLname{get;set;} publicvirtualICollection<Engagement>Engagements{get;set;}}}推荐答案
Hi Te rrence,
Hi Terrence,
问题是Code First默认配置所需关系的级联删除。在您的情况下,删除参与将导致通过参与级联删除人员 - >人与参与 - >练习 - > Person,SQL Server 不支持此功能。
The issue is that Code First configures cascade delete on required relationships by default. In your case deleting an Engagement would cause a cascade delete of Person through Engagement -> Person and Engagement -> Exercise -> Person, SQL Server does not support this.
您需要关闭其中一个级联删除,这里有一些Fluent API代码可以关闭练习 - >人级联删除;
You need to switch off one of the cascade deletes, here is some Fluent API code to switch off the Exercise -> Person cascade delete;
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Excercise>() .HasRequired(e => e.SurveyPerson) .WithMany() .HasConstraint((e, p) => e.InitalSiteSurveyPesonID == p.ID) .WillCascadeOnDelete(false); }
~Rowan
更多推荐
周期性参考问题?
发布评论