2
votes

I'm trying to work through a problem where I'm mapping EF Entities to POCO which serve as DTO.

I have two tables within my database, say Products and Categories. A Product belongs to one category and one category may contain many Products. My EF entities are named efProduct and efCategory. Within each entity there is the proper Navigation Property between efProduct and efCategory.

My Poco objects are simple

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; }
}

To get a list of products I am able to do something like

    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
               };
    }

However there is a type mismatch error because p.Category is of type efCategory. How can I resolve this? That is, how can I convert p.Category to type Category?

Likewise when I do

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

I get a mismatch because ProductList is of type Product, where c.Products is an EntityCollection

I know in .NET EF has added support for POCO, but I'm forced to use .NET 3.5 SP1.

1

1 Answers

4
votes
    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.
               }
           };

For Category you do:

    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
                              }
            };