c# - Entity Framework 6 insert into table with existing data throws DbUpdateException -
c# - Entity Framework 6 insert into table with existing data throws DbUpdateException -
i'm using c# entity framework 6 , mssql server. i'm trying insert info table existing historical data. below snippet of code, dbupdateexception "violation of primary key constraint". checked sql table , edmx, , both have right id column identity column. also, edmx's ssdl , csdl correctly set "identity".
update: sql table has data, savechanges() tries insert table identity starting 0, ignoring existing data, causing pk constraint violation error.
please assist.
public int persistentity(t entity) { var newrecords = 0; using (var context = new myentities()) { using (var dbcontext = context.database.begintransaction()) { var entity = new tblentity(); // ... context.tblentities.add(entity); //context.tblentities.attach(entity); // tried without success. seek { newrecords = context.savechanges(); dbcontext.commit(); } grab (dbupdateexception) { throw; // thrown on context.savechanges() } } } homecoming newrecords; }
here code tblentity:
namespace mylibrary.infrastructure.data { using system; using system.collections.generic; public partial class tblentity { public tblentity() { this.tblcalibrations = new hashset<tblcalibration>(); this.tbloutput = new hashset<tbloutput>(); } public int iid { get; set; } public string cplatename { get; set; } public int iinstrument { get; set; } public int istatus { get; set; } public int itest { get; set; } public virtual tbltest tbltest { get; set; } public virtual icollection<tblcalibration> tblcalibrations { get; set; } public virtual tblinstrument tblinstrument { get; set; } public virtual icollection<tbloutput> tbloutput { get; set; } public virtual tblstatus tblstatus { get; set; } } }
c# entity-framework-6
Comments
Post a Comment