与实体框架或LINQ串到SQL的映射集实体、框架、SQL、LINQ

2023-09-04 23:20:19 作者:爱无一身轻

我想知道是否有可能映射1-许多数据库关系作为父对象类基元的阵列。例如,假设我有一个数据库,如下所示:

I would like to know if it is possible to map a 1-many database relationship as an array of primitives on the parent object class. For example, say I have a database as follows:

TABLE: PERSON
    ID - int

TABLE: PERSON_EMAIL_ADDRESSES
    PERSON_ID - int
    EMAIL_ADDRESS - string

有关我的Person类,我想有以下几点:

For my Person class, I'd like to have the following:

public class Person 
{
    public int ID { get; set; }
    public string[] EmailAddresses { get; set; }
}

默认的LINQ to SQL和放大器;实体框架的行为将会给我的PERSON_EMAIL_ADDRESSES表一个单独的类,以及该类型的Person对象......在集合这是我不想要的东西。

The default Linq to SQL & Entity Framework behavior would be to give me a separate class for the PERSON_EMAIL_ADDRESSES table, and a collection of that type on the Person object...which is what I don't want.

看起来这可能与NHibernate的描述来完成here,但有什么办法做到这一点无论是与EF或LINQ to SQL的?

Looks like this can be done with NHibernate as described here, but is there any way to do this with either EF or Linq to SQL?

在此先感谢, 韦恩

推荐答案

如果你想有一个只读列表,与你的NHibernate的示例,您应该映射像往常一样,然后项目:

If you want a read-only list, as with your NHibernate sample, you should map as usual and then project:

var q = from p in Context.People
        select new Person // Where this is your Person type above, not a mapped entity type
        {
            ID = p.ID, 
            EmailAddresses = from pea in p.EmailAddresses
                             select pea.EmailAddress
        };

这工作在两种L2S和L2E。如果你想读写,那么你的需要的标识。

This works in both L2S and L2E. If you want read-write, then you need the ID.

 
精彩推荐
图片推荐