saveChanges - Sequence不包含匹配元素

时间:2013-08-09 09:22:32

标签: c# asp.net-mvc knockout.js entity breeze

我对breeze.js有点新鲜,我认为这个想法很棒,但我在这里被困了一下;每当我的breeze控制器试图保存它时,会抛出一个“Sequence包含无匹配元素”,而json包含数据;我正在使用knockout将它绑定到一个bootstrap模式,一切似乎都工作,直到saveChanges被触发并抛出我的错误。 json似乎是正确的(包含所有变化),我有点被困在这里;如果您需要一些代码,请告诉我

提前谢谢 扬

编辑添加的代码:

控制器

 [BreezeController]
    public class BreezeEntityController : ApiController
    {

         readonly BreezeEntityRepo repository = new BreezeEntityRepo();

         [HttpGet]
         public IQueryable GetEtCustomers()
         {
             if (User.Identity.IsAuthenticated)
             {
                 Int32 idUser = WebSecurity.GetUserId(User.Identity.Name);
                 Int32 idSstuser = Helpers.Extensions.GetSstCustomerId(User.Identity.Name);
                 return repository.Customers.Cast<EtCustomer>().Where(o => (o.IdMain.HasValue && o.IdMain == idSstuser) || o.IdUser == idUser).AsQueryable();
             }

             return null;
         }

        [HttpGet]  
        public string Metadata()  
        { 
            return repository.Metadata();  
        }

        [HttpPost]
        public SaveResult SaveChanges(JObject saveBundle)
        {
            return repository.SaveChanges(saveBundle);
        }  

    }

我想保存的json:

{
"entities": [{
    "IdSstCustomer": 12520,
    "IdAddressType": 40,
    "IdTitle": 10,
    "Lastname": "Lieferadresse",
    "Firstname": "Dummy",
    "Title": null,
    "Name": "Maximilian Jan",
    "Email": "test@test.at",
    "NLstop": null,
    "Phone": null,
    "Company": null,
    "UID": "",
    "Address": "Teststr 1",
    "Address2": null,
    "City": "Wien",
    "ZipCode": "1110",
    "State": null,
    "Country": "AT",
    "CreateDate": "2013-02-25T12:06:00Z",
    "CreateUser": 22,
    "UpdateDate": null,
    "UpdateUser": null,
    "ApplicationId": null,
    "UserId": null,
    "IdMain": 10080,
    "IdUser": null,
    "entityAspect": {
        "entityTypeName": "EtCustomer:#interfaceLibrary",
        "defaultResourceName": "tbl_sst_Customer",
        "entityState": "Modified",
        "originalValuesMap": {
            "Firstname": "Jan",
            "Lastname": "Maximilian Lieferadresse",
            "Address": "Wienerstraße 53a",
            "ZipCode": "4020",
            "City": "Linz",
            "Email": "test@test-vertrieb.at"
        },
        "autoGeneratedKey": {
            "propertyName": "IdSstCustomer",
            "autoGeneratedKeyType": "Identity"
        }
    }
}],
"saveOptions": {}

}

我的实体模型也在外部dll中;这可能是原因吗?

关于jan

编辑抱歉这里是最后一段代码,这是存储库:

 public class BreezeEntityRepo
    {
        readonly EFContextProvider<FaEntities> _contextProvider =
  new EFContextProvider<FaEntities>();

        public IQueryable<EtCustomer> Customers
        {
            get 
            {
                return _contextProvider.Context.tbl_sst_Customer; 
            }
        }

        public string Metadata()
        {
            return _contextProvider.Metadata();  
        }

        public SaveResult SaveChanges(JObject saveBundle)
        {
            return _contextProvider.SaveChanges(saveBundle);  
        }
    }
}

viewmodel是

   var vm = {
        people: ko.observableArray([]),
        hide: ko.observable(true),
        save: saveChanges,
        newEtCustomer: ko.observable(
          {
              Title: "",
              Firstname: "",
              Lastname: "",
              Email: "",
              Address: "",
              City: "",
              ZipCode: "",
              Country: ""
          }),
        addEtCustomer: addNewEtCustomer,
        editEtCustomer: editEtCustomer
    };

并保存名为

   function editEtCustomer(customer) {
        vm.save();
    }

很难将数据正确提交回服务器端控制器

关于jan

编辑:

好吧,堆栈跟踪是

  bei System.Linq.Enumerable.First[TSource](IEnumerable`1 source, Func`2 predicate)
   bei Breeze.WebApi.EFContextProvider`1.GetEntitySetName(Type entityType)
   bei Breeze.WebApi.EFContextProvider`1.ProcessSaves(Dictionary`2 saveMap)
   bei Breeze.WebApi.EFContextProvider`1.SaveChangesCore(SaveWorkState saveWorkState)
   bei Breeze.WebApi.ContextProvider.OpenAndSave(SaveWorkState saveWorkState)
   bei Breeze.WebApi.ContextProvider.SaveChanges(JObject saveBundle, TransactionSettings transactionSettings)
   bei WatShop.Models.Entity.BreezeEntityRepo.SaveChanges(JObject saveBundle) in c:\Projekte\ProjekteWeb\WatShop\WatShop\Models\Entity\BreezeEntityRepo.cs:Zeile 33.
   bei WatShop.Controllers.BreezeEntityController.SaveChanges(JObject saveBundle) in c:\Projekte\ProjekteWeb\WatShop\WatShop\Controllers\BreezeEntityController.cs:Zeile 44.
   bei lambda_method(Closure , Object , Object[] )
   bei System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
   bei System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   bei System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
   bei System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)

希望这会有所帮助

关于jan

2 个答案:

答案 0 :(得分:1)

我有同样的问题。我正在使用Entity Framework 4.0生成的EDMX文件。

问题在于以下几行:

// Old EDMX ObjectContext has empty OSpace, so we get cspaceEntityType directly
var cspaceEntityTypes = metaWs.GetItems<EntityType>(DataSpace.CSpace);
cspaceEntityType = cspaceEntityTypes.First(et => et.FullName == entityType.FullName);

在我的情况下et.FullName包含"MyModel.Customer"等值,但entityType.FullName包含"My.CSharp.Namespace.Customer"

所以它看起来像比较问题。快速(和脏)的解决方法是在coparison中仅使用部分而不是FullName:

cspaceEntityType = cspaceEntityTypes.First(et => et.Name == entityType.Name); 

更新:如果您不想更改BreezeJS代码,请按照https://stackoverflow.com/a/18001434/174638

中的说明更改EDMX命名空间

答案 1 :(得分:0)

我也有同样的错误。希望我早点发现这篇文章,因为我必须包含Breeze源代码并进行调试和修复。

我的解决方案是更改EDMX的命名空间。只要您没有执行任何实体SQL,这不会影响您现有的代码库。此外,您不必更改微风代码。

将EDMX命名空间从“[EntityModelName]”更改为C#命名空间。

Breeze将完美地运作。