Search code examples
c#asp.net-mvcknockout.jsentitybreeze

saveChanges - Sequence contains no matching element


I am kinda new to breeze.js and i tough the idea was great but I am stuck a bit here; everytime my breeze controller tries to save it throws me a "Sequence contains no matching element" altough the json contains the data; I am using knockout to bind it to a bootstrap modal and everything seems to work find till saveChanges is triggered and throws me that error. the json seems to be correct (contains all changes), I am kind of stuck here; if you need some code please let me know

thanks in advance jan

edit added code:

the controller

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

    }

the json I am trying to save:

{
"entities": [{
    "IdSstCustomer": 12520,
    "IdAddressType": 40,
    "IdTitle": 10,
    "Lastname": "Lieferadresse",
    "Firstname": "Dummy",
    "Title": null,
    "Name": "Maximilian Jan",
    "Email": "[email protected]",
    "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": "[email protected]"
        },
        "autoGeneratedKey": {
            "propertyName": "IdSstCustomer",
            "autoGeneratedKeyType": "Identity"
        }
    }
}],
"saveOptions": {}

}

also my entity model is within an external dll; could this be the reason?

regards jan

edit sorry here is the last piece of code, this is repository:

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

the viewmodel is

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

and the save is called

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

tough the data is commited correctly back to the server side controller

regards jan

edit:

okay the stacktrace is

  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)

hope this helps

regards jan


Solution

  • I have the same problem. I am using EDMX file generated by Entity Framework 4.0.

    The problem is in the following lines:

    // 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);
    

    In my case et.FullName conatins values such as "MyModel.Customer", but entityType.FullName contains "My.CSharp.Namespace.Customer".

    So it looks like comparison problem. A quick (and dirty) workaround is to use only partial, not FullName in coparison:

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

    UPDATE: If you do not want to change the BreezeJS code, change the EDMX namespace as described in https://stackoverflow.com/a/18001434/174638