周期性参考问题?

编程入门 行业动态 更新时间:2024-10-24 12:27:12
本文介绍了周期性参考问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

每个练习都有一个与之相关的人。

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

更多推荐

周期性参考问题?

本文发布于:2023-11-07 06:35:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:周期性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!