asp.net mvc 4 - Entity Framework Issue, Inserting into many different Tables -


hi need insert purchase order header, , details. inserting data purchaseorder table succesful when tried inserting data purchaseorderdetails inserted data many different tables. tried debugging didn't find problem. checked sql profiler found out ef inserting data ff tables" newproposal,institution,product,etc.

please me, how can prevent ef inserting data other tables, need ef insert data po , podetails table.

here's purchase order model:

public class purchaseorder {     [key]     public int purchaseorderid {get; set;}      [required]     public string purchaseorderno { get; set; }      [required]     public datetime purchaseorderdate { get; set; }      public datetime datecreated { get; set; }      public datetime datemodified { get; set; }      public bool isdraft { get; set; }      public int? institutionid { get; set; }      public virtual institution institution { get; set; }      [required]     public datetime receiveddate { get; set; }      public string remarks { get; set; }      public list<podetail> podetail { get; set; }  } 

here's purchase order details model:

public class podetail {     [key]     public int id { get; set; }       public virtual purchaseorder purchaseorder { get; set; }      public virtual uliv.viewmodels.newproposal proposal { get; set; }       public datetime datecreated { get; set; }      public datetime datemodified { get; set; }      public bool isdraft { get; set; }  } 

for reference also, newproposal vm:

public class newproposal {     [key]     [display(name="proposal id")]     public int proposalid { get; set; }      public int institutionid { get; set; }     [foreignkey("institutionid")]     public virtual institution institution { get; set; }      public string proposalcode { get; set; }       [display(name = "proposed unit price")]     ///[required]     public decimal proposedunitprice { get; set; }      ///[required]     [display(name = "proposed volume")]     public string proposedvolume { get; set; }      public string remarks { get; set; }      public int statusid { get; set; }     public virtual status status { get; set; }      public int vaccinetypeid { get; set; }     public virtual vaccinetype vaccinetype { get; set; }       public int productid { get; set; }     public virtual productmodel product {get; set;}       public datetime datemodified { get; set; }       public datetime datecreated { get; set; }      public decimal finalunitprice { get; set; }     public decimal finalvolume { get; set; }      public decimal finaltotalamount { get; set; }      public string relatedproposalcode { get; set; }      //public list<podetail> podetail { get; set; }      public string getdatestring     {         {             return datemodified.toshortdatestring();         }     }      public bool isdraft { get; set; }      public int userid { get; set; }      public string datedisplay { get; set; } } 

lastly create action saving purchase orders:

[httppost] public actionresult create(purchaseorderviewmodel purchaseorderviewmodel)     {          session["purchaseorderno"] = purchaseorderviewmodel.purchaseorderno;          purchaseorderviewmodel.addedproposal  = (list<uliv.viewmodels.newproposal>)session["addedproposal"];           int instituionid = convert.toint32(session["institutionid"]);                    purchaseorderviewmodel.isdraft = true;          purchaseorderviewmodel.datecreated = datetime.now;          purchaseorderviewmodel.datemodified = datetime.now;           if (modelstate.isvalid)         {              try             {                 uliv.models.purchaseorder newpo = new purchaseorder();                 newpo.institution = db.institutions.find(instituionid);                 newpo.purchaseorderno = purchaseorderviewmodel.purchaseorderno;                 newpo.purchaseorderdate = purchaseorderviewmodel.purchaseorderdate;                 newpo.receiveddate = purchaseorderviewmodel.receiveddate;                 newpo.remarks = purchaseorderviewmodel.remarks;                 //newpo.institution =                  newpo.datecreated = datetime.now;                 newpo.datemodified = datetime.now;                 newpo.isdraft = true;                 db.purchaseorders.add(newpo);                 db.savechanges();                  var query = (from m in db.purchaseorders orderby m.purchaseorderid descending select m).take(1).tolist();                   foreach (var d in purchaseorderviewmodel.addedproposal)                 {                     podetail newpodetail = new podetail();                       foreach (var in query)                     {                         newpodetail.purchaseorder = i;                      }                       newpodetail.proposal = d;                     newpodetail.datecreated = datetime.now;                     newpodetail.datemodified = datetime.now;                     newpodetail.isdraft = true;                      db.podetails.add(newpodetail);                     db.savechanges();                 }             }             catch (dbentityvalidationexception dbex)             {                 foreach (var validationerrors in dbex.entityvalidationerrors)                 {                     foreach (var validationerror in validationerrors.validationerrors)                     {                         trace.traceinformation("property: {0} error: {1}", validationerror.propertyname, validationerror.errormessage);                     }                 }             }               session["addedproposal"] = null;               return redirecttoaction("index","fulfillment");         }           viewbag.proposal = new selectlist(db.newproposal.where(x => x.status.statusid == 3), "proposalid", "proposalcode");         return view(purchaseorderviewmodel);     } 

you this:

newpodetail.proposal = d; 

with d being part of viewmodel. means setting proposal instance doesn't come dbcontext. because of that, ef thinks new thing (because doesn't know it) , insert.

what can tu find actual proposal dbcontext using db.proposals.find(d.id) or or use db.attach(d) attach object current dbcontext.

more information regarding attach: http://msdn.microsoft.com/en-us/library/system.data.entity.dbset.attach%28v=vs.103%29.aspx


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -