与实体框架或LINQ串到SQL的映射集 [英] Mapping collection of strings with Entity Framework or Linq to SQL

查看:149
本文介绍了与实体框架或LINQ串到SQL的映射集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能映射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的描述来完成<一href="http://stackoverflow.com/questions/606607/mapping-collection-of-strings-with-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.

这篇关于与实体框架或LINQ串到SQL的映射集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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