Nick Birke
Nick Birke

Reputation: 137

cannot see dbcontext methods when instantiating entity model ie savechanges

I am trying to build a solution with an EF entity model and a WCF service. In the wcf service I instantiate the entity model (which inherits DbContext). I can use it fine for querying but when I try to save changes I get "does not contain a definition for 'SaveChanges'" when calling it. Any ideas?

Here is the definiton of the model

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

and here is the code where the error occurs... I should not that all I can see in Intellisense are the entities that are defined (no other methods)

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

Any help is useful. This is from an example in a book (if something looks off I did not write this myself) :-)

Upvotes: 2

Views: 1759

Answers (1)

Nick Birke
Nick Birke

Reputation: 137

I just figured this out... I had to reference the EntityFramework.dll in the WCF project :-)

Upvotes: 1

Related Questions