实体框架5:代码第一循环的关系问题 [英] Entity Framework 5: Code-First Cyclical Relationship Issues

查看:118
本文介绍了实体框架5:代码第一循环的关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白为什么EF不允许在PK / FK的关系循环引用。我在寻找如何改变我的模型对以下情形工作的建议。

I understand why EF does not allow "cyclical references" in the PK/FK relationships. I am looking for advice on how to alter my model to make the following scenario work.

三实体:员工代理商 WorkRecord 。他们的目的是记录员工的时间花在工作。 员工则包含参照代理商他(她)正受雇于和/他/她的 WorkRecord 包含参照代理商的工作是为完成。

Three entities: Employee, Agency, WorkRecord. Their purpose is to log Employee time spent doing work. Employee then contains reference to the Agency he/she is employed by, and his/her WorkRecord contain reference to the Agency the work was done for.

public class Employee
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public int AgencyId { get; set; }
    public virtual Agency Agency { get; set; }

    public virtual IEnumerable<WorkRecord> WorkRecords { get; set; }
}

public class Agency
{
    [Key]
    public int Id { get; set; } 
}

public class WorkRecord
{
    [Key]
    public int Id { get; set; } 

    public int Hours { get; set; } 

    public int AgencyId { get; set; } 
    public virtual Agency Agency { get; set; } 

    public int EmployeeId { get; set; }
    public virtual Employee { get; set; }
}



喜欢这样做,母狗: FK_dbo.WorkRecords_dbo .Employees_EmployeeId 导致周期性的参考。

我的第一个念头是因为双向的虚拟特性,所以我决定指派的两位具有1路关系的顶层实体之一:

My first thought was because of the bi-directional virtual properties, so I decided to designate one of the two a top-level entity with a 1-way relationship:

首先,我指定 WorkRecord 作为一个顶级实体,并从员工 WorkRecords 参考参考C $ C>实体...产生相同的消息。

First, I designated WorkRecord as a top-level entity and remove the virtual WorkRecords reference reference from the Employee entity... the same message is produced.

二,我做了员工顶固级的实体,留下它的虚拟 WorkRecords 集合,从本虚员工引用属性 WorkRecord 实体...工作正常,但并没有达到我的目标。

Second, I made Employee the top-level entity, leaving its virtual WorkRecords collection, and removing the virtual Employee reference property from the WorkRecord entity... works fine but does not achieve my goal.

更多的调查之后,我发现这是原子能机构虚拟引用属性导致循环引用两个实体。如果一个实体移除此,在员工 / WorkRecord 实体关系在各个方向上工作。

After more investigation, I find it is the Agency virtual reference property on both entities that causes the circular reference. If one entity removes this, the Employee/WorkRecord entity relationships work in all directions.

所以,清楚,我可以问 - 我该如何表达这种商业模式,使用 WorkRecord 我的顶层实体,未做EF5心烦?

So, clear as i can ask - how can I express this business model, using WorkRecord as my top-level entity, without making EF5 upset?

推荐答案

这听起来像你只是想获得EF过你的背,但我认为它实际上是在您的数据的耦合表达一个有效的问题。如果您绑定AgencyId既WorkRecord和员工再更新AgencyId上WorkRecord,例如,将级联到员工。然后将级联到WorkRecord等。因此循环引用。你真的应该指定其中的数据对象将自己给代理商的关系。

It sounds like you just want to get EF off your back, but I think it's actually expressing a valid problem in the coupling of your data. If you bind AgencyId to both WorkRecord and Employee then updating the AgencyId on WorkRecord, for example, will cascade to Employee. Which will then cascade to WorkRecord etc. Hence "circular reference". You really should designate which of those data objects will "own" the relationship to Agency.

就个人而言,我怀疑是最自然的结合是从WorkRecord引用局。我可以看到一个场景,其中一个员工可能会从一个机构转移到另一个,但它会是非常困难的WorkRecord从一处移动到另一个。这也是,如果没有WorkRecord的员工不能真正被称为多雇员,果然如此。如果你确定这是的话,那么我会除去员工机构参考。如果你需要从员工到该机构,那么你可能的 的应该经过WorkRecord无妨。

Personally, I suspect that the most natural binding is to reference the Agency from the WorkRecord. I can see a scenario where an Employee might move from one agency to another but it'd be much harder for a WorkRecord to move from one Agency to another. It's also the case that an Employee without a WorkRecord can't really be termed much of an Employee, really. If you determine this to be the case, then I'd remove the Agency reference from Employee. If you need to get to the Agency from the Employee then you probably should go through a WorkRecord anyway.

所有这一切仅仅是概念上的,然而。我猜想,如果你把它可能AgencyId为空对员工的EF不会抱怨任何延长(与你的可能的希望选两个)。这应该做出有效的无的需要进行更新一个Employee它的有WorkRecord圆形更新。我不得不测试来验证,但我怀疑它会成立。

All of that is merely conceptual, however. I suspect that if you make it possible for AgencyId to be null on the Employee that EF won't complain any longer (and you might want it optional on both). That should make it valid for an Employee to be updated without requiring a circular update with WorkRecord. I'd have to test that to verify, but I suspect it'd hold true.

public class Employee
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public int? AgencyId { get; set; }
    public virtual Agency Agency { get; set; }

    public virtual IEnumerable<WorkRecord> WorkRecords { get; set; }
}

这篇关于实体框架5:代码第一循环的关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆