c# - EF7 beta6: Error saving multiple entities -


i creating api using asp.net5 , entity framework 7.0.0-beta 6, , when try execute various updates in several requests, exception:

'company' cannot tracked because instance of type same key being tracked. new entities consider using iidentitygenerator generate unique key values.

this code:

public class mrbellhopcontext : dbcontext {      public dbset<company> company { get; set; }      protected override void onmodelcreating(modelbuilder modelbuilder)     {         modelbuilder.entity<company>(entity =>         {             entity.key(c => c.companyid);              entity.index(c => c.name);              entity.property(c => c.companyid).valuegeneratedonadd();         });          modelbuilder.usesqlserveridentitycolumns();          base.onmodelcreating(modelbuilder);     }  }   public class company {      public int companyid { get; set; }      public string name { get; set; }      public string description { get; set; }      public string phone { get; set; }      public string email { get; set; }      public short statusid { get; set; }  }   public class companyrepository : icompanyrepository {      mrbellhopcontext _dbcontext;       public async task updateasync(company company)     {         _dbcontext.update(company);         await _dbcontext.savechangesasync();     } }    [route("api/[controller]")] public class companycontroller : controller {       [httpput]     public async void updateasync([frombody] company company)     {         if ((!modelstate.isvalid) || (company == null))         {             context.response.statuscode = 400;             return;         }         else         {             await _repository.updateasync(company);         }     }  } 

i have tried solve removing valuegeneratedonadd(), usesqlserveridentitycolumns() or changing mapping, if try update several entities in several requests, exception:

  1. first req: update companyid 8
  2. first req: update companyid 9 !! error

does know how solve issue?

resolved: https://github.com/aspnet/entityframework/issues/2652

i adding repository singleton:

services.addsigleton<data.interfaces.company.icompanyrepository,data.repositories.company.companyrepository>(); 

this means requests sharing single instance of repository. should reduce scoped, have single repository instance per request. aside avoiding issue hitting, ensure don't end gigantic context instance tracking data database in memory.

to solve:

services.addscoped<data.interfaces.company.icompanyrepository,data.repositories.company.companyrepository>(); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -