【问题标题】:cannot see dbcontext methods when instantiating entity model ie savechanges实例化实体模型时看不到 dbcontext 方法,即 savechanges
【发布时间】:2014-02-04 20:28:47
【问题描述】:

我正在尝试使用 EF 实体模型和 WCF 服务构建解决方案。在 wcf 服务中,我实例化了实体模型(它继承了 DbContext)。我可以很好地使用它进行查询,但是当我尝试保存更改时,我在调用它时得到“不包含'SaveChanges'的定义”。有什么想法吗?

这是模型的定义

    namespace ProductsEntityModel
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class AdventureWorksEntities : DbContext
    {
        public AdventureWorksEntities()
            : base("name=AdventureWorksEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<Product> Products { get; set; }
        public DbSet<ProductInventory> ProductInventories { get; set; }
    }
}

这是发生错误的代码......我不应该在 Intellisense 中看到的只是定义的实体(没有其他方法)

        public bool ChangeStockLevel(string productNumber, short newStockLevel, string shelf, int bin)
    {
        try
        {
            AdventureWorksEntities database = new AdventureWorksEntities();

            int productID = (from Product p in database.Products
                             where string.Compare(p.ProductNumber, productNumber) == 0
                             select p.ProductID).First();

            ProductInventory productInventory = database.ProductInventories.First(
                pi => String.Compare(pi.Shelf,shelf) == 0 &&
                    pi.Bin == bin &&
                    pi.ProductID == productID
                );

            productInventory.Quantity += newStockLevel;

            database.SaveChanges()  //CANNOT SEE SAVECHANGES HERE
        }
        catch
        {
            return false;
        }

        return true;
    }

任何帮助都是有用的。这是来自书中的一个例子(如果看起来有些不对劲,我不是自己写的):-)

【问题讨论】:

    标签: c# wcf entity-framework entity


    【解决方案1】:

    我刚刚想通了...我不得不在 WCF 项目中引用 EntityFramework.dll :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 2016-01-30
      相关资源
      最近更新 更多