EF:实体类型不是使用存储过程的当前上下文的模型的一部分



我正在使用EF5。我使用了代码优先的方法。我在使用存储过程时出错。错误为

"The entity type CustomProduct is not part of the model for the current context."

Db中有3个表。

  • 产品

    • 产品ID
    • ProductName
    • 产品描述
  • ProctVaraint

    • 产品VaraintId
    • 产品ID
    • 产品油漆名称
    • 股票
    • 尺寸
  • 产品价格

    • 产品价格Id
    • ProudctId
    • 价格

每个实体都有单独的类,具有所有属性。

  • 产品.cs
  • 产品油漆.cs
  • 产品价格.cs

这是所有类别的

public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
    }
    public class ProductVaraint
    {
        public int ProductVaraintId { get; set; }
        public int ProductId { get; set; }
        public string ProdcutVaraintName { get; set; }
        public int Stock { get; set; }
        public int Size { get; set; }
    }
    public class ProductPrice
    {
        public int ProductPriceId { get; set; }
        public int ProductId { get; set; }
        public decimal Price { get; set; }
    }
    public class CustomProduct
    {
        public int ProudctId { get; set; }
        public string ProductName { get; set; }
        public string ProductDescription { get; set; }
        public int Stock { get; set; }
        public int Size { get; set; }
        public decimal Price { get; set; }
    }

这是存储的过程

   public IList<CustomProduct> ExecuteSP()
    {
        var context = ((IObjectContextAdapter)(this)).ObjectContext;
        var connection = this.Database.Connection;
            //open the connection
            if (connection.State == ConnectionState.Closed)
                connection.Open();
            //create a command object
            using (var cmd = connection.CreateCommand())
            {
                //command to execute
                cmd.CommandText = "GetProducts";
                cmd.CommandType = CommandType.StoredProcedure;
                var reader = cmd.ExecuteReader();
                var result = context.Translate<CustomProduct>(reader).ToList();
                for (int i = 0; i < result.Count; i++)
                    result[i] = AttachEntityToContext(result[i]);
                reader.Close();
                return result;
            }
        }
    }

public TEntity AttachEntityToContext<TEntity>(TEntity entity) where TEntity : BaseEntity, new()
        {
var alreadyAttached = Set<TEntity>().Local.Where(x => x.Id == entity.Id).FirstOrDefault();
            if (alreadyAttached == null)
            {
                Set<TEntity>().Attach(entity);
                return entity;
            }
            else
            {
                return alreadyAttached;
            }
        }

我已经用DBContext映射了Product.cs、ProductVaraint.cs和ProductPrice.cs,但没有映射CustomProduct.cs

现在,我已经制作了一个存储过程来返回ProductName(来自Product)、ProductDescription(来自Product)、Stock(来自ProductVaraint)、Size(来自ProductVaraint)和Price(来自ProductPrice)。

为了映射这些属性,我有一个名为CustomProudct.cs的单独类,它包含存储过程返回的所有属性,并用存储过程映射这个类。

CustomProduct没有单独的表,我不需要只为映射sp结果创建额外的表。

我知道原因是没有CustomProduct的单独表,EF正试图在Db中搜索该表,但没有找到任何表,这就是它抛出异常的原因。

请有人建议我怎么做。有没有其他方法来处理这种情况?

您应该有类似的东西

List<CustomProduct> lst = new List<CustomProduct>();
 while reader.Read()
          {
             CustomProduct cp = new CustomProduct();
             cp.ProductName = reader.GetString(0); --where 0 is the column index
             .....
             lst.Add(cp);
          }

我不知道AttachEntityToContext做什么,但顾名思义,您正试图将此CustomProduct附加到DbContext。它将不起作用,因为这是您创建的一个类,在DB中没有对应程序。

相关内容

最新更新