【问题标题】:EFv1 mapping 1 to many Relationship to POCOsEFv1 映射 1 到多 与 POCO 的关系
【发布时间】:2011-03-01 21:09:10
【问题描述】:

我正在尝试解决将 EF 实体映射到用作 DTO 的 POCO 的问题。

我的数据库中有两个表,分别是 Products 和 Categories。一个产品属于一个类别,一个类别可能包含许多产品。我的 EF 实体被命名为 efProduct 和 efCategory。在每个实体中,efProduct 和 efCategory 之间都有适当的导航属性。

我的 Poco 对象很简单

public class Product
{
    public string Name { get; set; }
    public int ID { get; set; }
    public double Price { get; set; }
    public Category ProductType { get; set; }
}

public class Category
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Product> products { get; set; }
}

要获得产品列表,我可以做类似的事情

    public IQueryable<Product> GetProducts()
    {
        return from p in ctx.Products
               select new Product
               {
                   ID = p.ID,
                   Name = p.Name,
                   Price = p.Price
                   ProductType = p.Category
               };
    }

但是,由于 p.Category 的类型为 efCategory,因此存在类型不匹配错误。我该如何解决这个问题?即如何将 p.Category 转换为 Category 类型?

当我这样做时也是如此

        return from c in ctx.Categories
                where c.ID == id
                select new Category
                {
                    ID = c.ID,
                    Name = c.Name,
                    ProductList = c.Products;
                };

我得到一个不匹配,因为 ProductList 是 Product 类型,其中 c.Products 是一个 EntityCollection

我知道 .NET EF 增加了对 POCO 的支持,但我不得不使用 .NET 3.5 SP1。

【问题讨论】:

    标签: c# entity-framework .net-3.5 poco dto


    【解决方案1】:
        return from p in ctx.Products
               select new Product
               {
                   ID = p.ID,
                   Name = p.Name,
                   Price = p.Price
                   ProductType = new Category
                   {
                       ID = p.Category.ID,
                       Name = p.Category.Name // etc.
                   }
               };
    

    对于Category,您可以这样做:

        return from c in ctx.Categories
                where c.ID == id
                select new Category
                {
                    ID = c.ID,
                    Name = c.Name,
                    ProductList = from p in c.Products
                                  select new Product
                                  {
                                      ID = p.ID,
                                      Name = p.Name,
                                      Price = p.Price
                                  }
                };
    

    【讨论】:

    • 谢谢!你能给我一个关于如何返回类别的例子吗?这里的产品是一个 List,在 efCategory 中它是一个 EntityCollection?我更新了问题以反映这一点。
    • 好的;我为此添加了一个示例。
    • 但是,如果我想在一个地方将 EF 模型“转换”为我自己的模型怎么办?例如from c in ctx.Categories select c.ToPoco(); 这在 L2S 中可以正常工作,但在 EF 中则不行,因为我会有“LINQ to Entities 无法识别该方法”......“方法无法转换为商店表达式”
    • 那么 ProductList 的延迟加载呢??
    • @Wahid,对于这个问题,延迟加载是不必要且低效的。我给出的答案按原样工作,无论是否打开延迟加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2019-04-07
    • 1970-01-01
    相关资源
    最近更新 更多