本文介绍了实体框架只读集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个域名,其中一个客户,公司,员工,等等等等,有一个物业的ContactInfo这反过来又包含了一组地址(ES),电话(ES),电子邮件(S),等等等等......

Consider a domain where a Customer, Company, Employee, etc, etc, have a ContactInfo property which in turn contains a set of Address(es), Phone(es), Email(s), etc, etc...

下面是我的缩写的ContactInfo:

Here is my abbreviated ContactInfo:

public class ContactInfo : Entity<int>
{
    public ContactInfo()
    {
        Addresses = new HashSet<Address>();
    }

    public virtual ISet<Address> Addresses { get ; private set; }

    public Address PrimaryAddress
    {
        get { return Addresses.FirstOrDefault(a => a.IsPrimary); }
    }

    public bool AddAddress(Address address)
    {
        // insure there is only one primary address in collection
        if (address.IsPrimary)
        {
            if (PrimaryAddress != null)
            {
                PrimaryAddress.IsPrimary = false;
            }
        }
        else
        {
            // make sure the only address in collection is primary
            if (!Addresses.Any())
            {
                address.IsPrimary = true;
            }
        }
        return Addresses.Add(address);
    }
}

一些注意事项(我不是100%肯定,如果这些是EF最佳实践):

Some notes (I am not 100% sure if these are EF "best practices"):


  • 地址(ES)的集合是虚拟的,允许延迟加载

  • 在收集私人二传手禁止收集更换

  • 集是的ISet ,以确保有每个触点没有重复的地址

  • 使用 AddAddress 方法我能保证总是有至多1的地址是主要的....

  • collection of Address(es) is virtual to allow for lazy loading
  • private setter on collection prohibits collection replacement
  • collection is an ISet to insure that there are no duplicate addresses per contact
  • using AddAddress method I can insure that there is always and at most 1 address which is primary....

我想(如果可能的话),以prevent通过添加地址ContactInfo.Addresses.Add()方法,并使用的给力ContactInfo.AddAddress(地址地址) ...

I would like (if possible) to prevent adding Addresses via ContactInfo.Addresses.Add() method and to force using of ContactInfo.AddAddress(Address address)...

我想通过 ReadOnlyCollection还露出的地址集,但将与实体框架(V5)?这项工作

I am thinking exposing the set of addresses via ReadOnlyCollection but will this work with Entity Framework (v5)?

我怎么会去吗?

推荐答案

的一种方法是使保护ICollection的财产,并创建了IEnumerable的一个新的属性,只是返回的ICollection属性的列表。

One way is to make the ICollection property protected and create a new property of IEnumerable that just returns the list of the ICollection property.

这样做的缺点是,你是不是能够通过的ContactInfo上的地址进行查询得到一样生活在这个城市的所有CONTACTINFO。

The downside with this is that you are not able to query on addresses through the ContactInfo like get all contactinfo that lives in this city.

这是不可能的:

from c in ContactInfos
where c.Addresses.Contains(x => x.City == "New York")
select c

code:

public class ContactInfo : Entity<int>
{
    public ContactInfo()
    {
        Addresses = new HashSet<Address>();
    }

    protected virtual ISet<Address> AddressesCollection { get ; private set; }

    public IEnumerable<Address> Addresses { get { return AddressesCollection; }}

    public Address PrimaryAddress
    {
        get { return Addresses.FirstOrDefault(a => a.IsPrimary); }
    }

    public bool AddAddress(Address address)
    {
        // insure there is only one primary address in collection
        if (address.IsPrimary)
        {
            if (PrimaryAddress != null)
            {
                PrimaryAddress.IsPrimary = false;
            }
        }
        else
        {
            // make sure the only address in collection is primary
            if (!Addresses.Any())
            {
                address.IsPrimary = true;
            }
        }
        return Addresses.Add(address);
    }
}

这篇关于实体框架只读集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-25 10:07