1
votes

I am getting the following error when saving Data, Get works fine. I am using VS2012,Creating MVC4 Webapi project, using Nuget to get the Breeze.js. On the server i am using DbContext from Code First.

ServerSide Code

 [BreezeController]
    public class CountryController : ApiController
    {
        private CountryContext db = new CountryContext();


            readonly EFContextProvider<CountryContext> _contextProvider =
        new EFContextProvider<CountryContext>();

        // ~/api/todos/Metadata 
        [HttpGet]
        public string Metadata()
        {
            return _contextProvider.Metadata();
        }

        // GET api/Country
        public IQueryable<Country> GetCountries()
        {
            return db.Countries.AsQueryable();
        }

        // ~/api/todos/SaveChanges
        [AcceptVerbs("POST")]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            return _contextProvider.SaveChanges(saveBundle);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

DbContext

public class CountryContext : DbContext
    {

        public CountryContext() : base("name=CountryContext")
        {
        }

        public DbSet<Country> Countries { get; set; }
    }

Header

{
   "entities":[
      {
         "Country_ID":1,
         "Country_Code":"USA modified",
         "Country_Name":"Unites states hjk",
         "entityAspect":{
            "entityTypeName":"Country:#LearnKnockout.Models",
            "entityState":"Modified",
            "originalValuesMap":{
               "Country_Name":"Unites states"
            },
            "autoGeneratedKey":{
               "propertyName":"Country_ID",
               "autoGeneratedKeyType":"Identity"
            }
         }
      }
   ],
   "saveOptions":{
      "allowConcurrentSaves":false
   }
}

Response

{
   "$id":"1",
   "$type":"System.Web.Http.HttpError, System.Web.Http",
   "Message":"An error has occurred.",
   "ExceptionMessage":"Method not found: 'System.Data.Objects.ObjectContext System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()'.",
   "ExceptionType":"System.MissingMethodException",
   "StackTrace":" at Breeze.WebApi.EFContextProvider 1.get_ObjectContext()\r\n at Breeze.WebApi.EFContextProvider 1.ProcessSaves(Dictionary2 saveMap)\r\n at Breeze.WebApi.EFContextProvider 1.SaveChangesCore(Dictionary2 saveMap)\r\n at Breeze.WebApi.ContextProvider.SaveChanges(JObject saveBundle)\r\n at LearnKnockout.Controllers.CountryController.SaveChanges(JObject saveBundle) in c:\\Users\\nssidhu\\Documents\\Visual Studio 2012\\Projects\\CountryWebAPI\\CountryWebAPI\\Controllers\\CountryController.cs:line 43\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.b__c(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.b__4()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func 1 func, CancellationToken cancellationToken)"
}
5
Are you using code first or database first EF? If database first, are you using a DbContext instance or an ObjectContext instance in your EFContextProvider<T>?Jay Traband
What do you exactly want? You didn't ask any questions more info besides a stacktrace would be helpfull...Michel Tol
On the server, what does your DbContext or ObjectContext look like and what version of Entity Framework are you using?Jay Traband
<package id="Breeze.MVC4WebApi" version="0.80.1" targetFramework="net45" /> <package id="EntityFramework" version="6.0.0-alpha2" targetFramework="net45" />NSS
I am finding that exact same error is occuring in the Todo sample as well, i downloaded to the sample using nuget as wellNSS

5 Answers

2
votes

I have a guess about how you got into this mess.

First, I'm betting that you are using the visual "Manage NuGet Packages" dialog within Visual Studio.

Second, I'll bet that you have selected "Include Prerelease" in the upper-left combobox. This is the only way I can reproduce your experience. Would you please check that combobox setting and comment back here for us?

If so, NuGet will install EF v.6 alpha if EF is not already in the project or upgrade EF 5 to EF 6 if it is. Ouch! This can also happen if you upgrade the EF package of an existing project separately.

I strongly recommend that everyone stick to the "Stable Only" setting unless you're absolutely certain you want a pre-release package; and then please remember to set it back to "Stable Only" as the combobox setting is "sticky".

Yes we could limit the upper bound of the EF version in our NuGet package ... and maybe we will. Frankly, that is contrary to the NuGet guidance, it's quite difficult to manage all of the versioning permutations, and it's important to take responsibility when deviating from the standard path.

What is unclear to me is how you acquired an older version of the Breeze.WebApi.dll that lacks the BreezeControllerAttribute. How did you learn of the [BreezeController] attribute except by installing the Breeze.MVC4WebApiClientSample NuGet package? That's the only NuGet package in which that attribute can be found ... and installing (or upgrading to) that package should have delivered the correct Breeze.WebApi.dll reference to the companion "packages" folder.

I suspect that your project file is a bit of a wreck. You're in NuGet hell and, unless you really know NuGet well, you're often better off rebuilding the project from scratch, copying in your custom code.

2
votes

Updated 10/28/2013:

As of now, Breeze 1.4.5 has support for Microsoft's ASP.NET WebApi 2 and Entity Framework 6. Please see http://www.breezejs.com/documentation/download.

Old post...

We have not yet tested Breeze with the Entity Framework version 6 alpha and are probably unlikely to do so until it reaches at least beta.

That said, the JsonFormatter and ODataActionFilter attributes still exist in the most recent versions of breeze alongside the BreezeController attribute.

1
votes

The missing method exception is thrown because of the namespace changes in EF 6.0. Specifically these:

System.Data.Objects.ObjectContext => System.Data.Entity.Core.Objects.ObjectContext System.Data.EntityState => System.Data.Entity.EntityState

more info: http://entityframework.codeplex.com/wikipage?title=Updating%20Applications%20to%20use%20EF6

You could grab the source and modify EFContextProvider.cs to use those namespaces.

0
votes

seems like there is problem with latest Nuget Packagae(mostly with alpha version of entity FramewroK).

I uninstalled the current BreezeNuget Package and then installed the earlier version and everything is working fine(using older version i had to change attributes to Jsonformater,oDataActionfilter instead of Breezecontroller)

Install-Package Breeze.MVC4WebApi -Version 0.78.1

0
votes

As ckal describes, you must update the EFContextProvider.cs and use the new namespaces in Entity Framework 6.

Here is a gist with the updated code. You have to update to Entity Framework 6 in Breeze.WebApi project to get it to work.

I have tested it in my own projects and it seems to work just as before.