开发者

Connection error with Entity framework and class product from data layer

Goal:

To use entity framework with N-tier in my WPF application.

Problem:

I can't merge the class Product from the map ProductRepository to entity framework that also has a class named Product.

When I tried solving the problem I always retrieve this error message:

Error 1 Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List' D:\Arbete\kurser\C#.NET\Labbar\Lab3\ny\MediaStore\DataAccessLibrary\ProductRepository\ProductRepository.cs 45 20 DataAccessLibrary

Just a little reminder: I would like to the class Product to be flexible that also can be used in business and presentation layer.

namespace DataAccessLibrary.ProductRepository
{

    public partial class Product
    {
        public Int32 ArticleNumber_id { get; set; }
   开发者_开发问答     public string Name { get; set; }
        public decimal SalePrice { get; set; }
        public decimal PurchasePrice { get; set; }
        //public string Book_url { get; set; }
        public Int32 ProductCategory_id { get; set; }
        public Int32 Supplier_id { get; set; }
        public Int32 Role_id { get; set; }

    }
}

namespace DataAccessLibrary.ProductRepository
{

    /// <summary>
    /// Responsible for uppdating, adding, deleting, retrieving data from product list.
    /// </summary>
    public class Productrepository : IProductrepository
    {

        private List<Product> myProductList;

        private MediaStoreEntities _myMediaStoreEntities = new MediaStoreEntities();

        public Productrepository()
        {
            myProductList = new List<Product>();
        }

        /// <summary>
        /// Retrieve all data from the product list
        /// </summary>
        /// <returns>A list with full of product data.</returns> 
        public List<Product> GetAllProductList()
        {
            var productListt = (from a in _myMediaStoreEntities.Products
                                          select a).ToList();

            return productListt;
            //return productList;   
        }
    }
}

Class: ProductRepository

Namespace: DataAccessLibrary.ProductRepository

/// <summary>
/// Retrieve all data from the product list
/// </summary>
/// <returns>A list with full of product data.</returns> 
public List<Product> GetAllProductList()
{
    var productListt = (from a in _myMediaStoreEntities.Products
                                  select a).ToList();

    return productListt;
    //return productList;   
}

Connection error with Entity framework and class product from data layer

Connection error with Entity framework and class product from data layer

Connection error with Entity framework and class product from data layer


That is because you have two Product classes. One in ProductRepository namespace and second in Database namespace. Both partial parts must be in the same namespace to form a single class.

If you are trying to use custom POCO class (only EFv4) you must turn off automatic code generation (you didn't because you have still .Designer.cs file under your EDMX) by removing custom tool in the designer. Then you must create custom context class derived from ObjectContext which will expose ObjectSet<DataAccessLibrary.ProductRepository.Product>


You have two classes, one Product.cs and one class is inside your EDMX's code behind file. And both are under different namespace.

For you , ProductRepository.Product class and EDMXNamespace.Product may look same, but for compiler, both classes are different,

List<ProductRepository.Product> != List<EDMXNamespace.Product>

There is no need to create Product.cs class at all in your ProductRepository as you are having all same fields, even if you want to create separate class, you should use some sort of constructor to copy all fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜