简体   繁体   English

在双向关系和实体框架中保持完整性

[英]maintaining integrity in bidirectional relations and Entity Framework

I am looking for a best-practice regarding the maintaining of bidirectional relations between C# POCO's that are being persisted with Entity Framework. 我正在寻找关于保持与实体框架保持一致的C#POCO之间的双向关系的最佳实践。

A Family has zero to multiple familyMembers and a Person always has one family. 一个家庭有零到多个家庭成员,一个人总是有一个家庭。 Now when I instantiate a new Person and a new Family: 现在,当我实例化一个新的人和一个新的家庭时:

Person john = new Person();
Family adams = new Family();

I would then add the person to the family and vice versa. 然后,我将该人添加到家庭中,反之亦然。 This takes: 这需要:

adams.familyMembers.Add(john);
john.family = adams;

I am wondering if there is some better way to do this, because if I add a Person to a Family, that Persons family property should always be set to that family. 我想知道是否有更好的方法可以这样做,因为如果我将一个人添加到一个家庭中,则该人的家庭财产应始终设置为该家庭。 If a developer forgets to do this you would end up with orphan objects. 如果开发人员忘记执行此操作,则最终将得到孤立对象。

Do I use full getters and setters to do this? 我是否使用完整的getter和setter来做到这一点? Is there some automated way to do this? 有一些自动的方法可以做到这一点吗?

Are there any special considerations when using Entity Framework to store these objects? 使用实体框架存储这些对象时,有什么特别的注意事项吗?

UML图

Thanks in advance 提前致谢

I added this Answer since you asked about integrity checking. 因为您询问完整性检查,所以我添加了此答案。 This feature in EF is widely overlooked. EF中的此功能被广泛忽略。

Your POCOs can implement IValidatableObject 您的POCO可以实现IValidatableObject

  public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext) 

Validate is called by EF on SaveChanges unless you have disabled it. 除非已禁用,否则EF将在SaveChanges上调用Validate

Context.Configuration.ValidateOnSaveEnabled = true; //default 

edit: SAMPLE CODE to get you started 编辑:示例代码,以帮助您入门

 // Sample Implementation. 
         var validationResult = base.Validate(validationContext).ToList();  
        // validationResult.AddRange(xxx); // an example of mini call for common validations


         if (true) // the condition that leads to a validation error
         {
             var memberList = new List<string> { "PropertyName" }; // the name of the offending property
             var error = new ValidationResult("Error text goes here", memberList); // 
             validationResult.Add(error);
         }

         return validationResult;

Entity framework could care less whether you set the other direction or not, it will still save it correctly. 不管您是否设置其他方向,实体框架都不会在乎,它仍会正确保存。 However, when using your domain model, you may want access to both navigation properties right away. 但是,在使用域模型时,您可能希望立即访问两个导航属性。 This requires setting the property as you are doing. 这需要在执行操作时设置属性。 The solution I use for nearly all navigation collections is the following 我用于几乎所有导航集合的解决方案如下

public class Family
{
    public Family()
    {
        this.FamilyMembers = new List<Person>();
    }
    public IEnumerable<Person> FamilyMembers {get; protected set;}

    public void AddFamilyMember(Person person)
    {
        this.FamilyMembers.Add(person);
        person.Family = this;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM