asp.net mvc - MVC 5 EF 6 Insert Multiple Rows SaveChanges -
i trying insert multiple rows using following code. savechanges() works first record if fails when tries next one. guess each time trying insert records. not sure if correct or not, , if correct how can change code make correct. following code: contrroller:
foreach(var m in pms) { _pmservice.addpms(m); }
and in _pmservice.addpms:
_pmrepository.add(pm); _pmrepository.savechanges();
_pmrepository.add (actually baserepository , pmrepository extends base)
_ctx.set<t>().add(entity);
finally, here savechanges() (again baserepostiroy)
_ctx.changetracker.detectchanges(); return _ctx.savechanges();
so in controller, looping through each entry, , calling add. add in service calls add in baserepository , savechanges(). first record works without issues 2nd record onwards fails. , following error:
"saving or accepting changes failed because more 1 entity of type xxxxxxxxxxxxx have same primary key value. ensure explicitly set primary key values unique. ensure database-generated primary keys configured correctly in database , in entity framework model. use entity designer database first/model first configuration. use 'hasdatabasegeneratedoption\" fluent api or 'databasegeneratedattribute' code first configuration."
and using databasefirst approach.
i found answer storegeneratedpattern. had set identity in edmx file primary key field , worked.
storegeneratedpattern="identity"
Comments
Post a Comment