保存数据时出现以下错误,Get 工作正常。我正在使用 VS2012,创建 MVC4 Webapi 项目,Nuget
用于获取Breeze.js
. 在服务器上,我使用 Code First 中的 DbContext。
服务器端代码
[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);
}
}
}
数据库上下文
public class CountryContext : DbContext
{
public CountryContext() : base("name=CountryContext")
{
}
public DbSet<Country> Countries { get; set; }
}
标题
{
"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
}
}
回复
{
"$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)"
}