c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -


In brief, the wrapper module is inserted during posting and the status of an entry is changed to 'modified' . Before changing the state, the state is set to 'separate', but calling attachments () causes the same error to be thrown away. I am using EF 6.

Model

  / wrapper class public class avi model {public A is a {get; Set; } Public listing & lt; B & gt; B {received; Set; } Public cc {receipt; Set; }}   

Administrator

  edit public functioning (int? id) {if (id == null) {new HttpStatusCodeResult (HttpStatusCode.BadRequest); } If (! Conveyor Access (id.Value)) new HTTPTitus code result (HTTP status code. Forbidden); Var aViewModel = new AViewModel (); aViewModel.A = db.As.Find (id); If (aViewModel.Receipt == zero) {return HttpNotFound (); } aViewModel.b = db.Bs.Where (x => x.aid == id.Value) .Oolist (); AViewModel.Vendor = db.Cs.Where (x => x.cid == aViewModel.a.cID) .FirstOrDefault (); See Return (aViewModel); } [HTPFost] [Valid AntitiferousTeacon] Public Action Result Advocate (Aviomodel Evumodel) {If (! Canoeasarassea (Evumodel.A.A.D.)) || Aviadel.A. USIDID! = Web Security.NETUSIRID (UserIndentity.Name)) New HTTTTTcodRist (HttpStatusCode.Forbidden); If (ModelState.IsValid) {db.Entry (aViewModel.a) .set = EntityState.Modified; // This is where the error is getting db.SaveChanges (); Return Redirect Action ("Index"); } See Return (aViewModel); }   

shown as a line

  db.Entry (aViewModel.a) .set = EntityState.Modified;   

Exposes exception:

An attached unit of type 'A' was unsuccessful because another unit of the same type is already primary The key is that this can happen when there is conflicting value in any unit in the graph, using the 'attach' method or setting the status of an entity to 'unchanged' or 'modified'. . This can happen because some organizations are new and the database values ​​generated from the database are not yet received. In this case, use the 'add' method or 'aided' unit state to track the graph and then set the status of non-new entities as 'unchanged' or 'modified' as appropriate.

Does anyone see something wrong in my code or understand what situation would it throw such an error during editing a model? >

method might possibly help someone but this will not help in this situation because the document was already being tracked when the document was being edited in the controller. . Attach will throw the exact same error.

The reason I encounter here is due to the canUserAccessA () that loads the unit before updating the position of the object. This tracked unit had to be screwed and it was different .

The object was changing for canUserAccessA () was to be modified, the object I was loading would not be tracked. The function AsNoTracking () should be called while doing a reference query.

  // user - & gt; Receipt verification private bool canUserAccessA (int AID) {int userID = WebSecurity.GetUserId (User.Identity.Name); int aFound = db.Model.AsNoTracking (). Where (x = & gt; x.aid ==aid & amp; amp; x.userID == userID) .count (); Return (aFound> 0); // if aFound & gt; 0, then come back to the truth, then return false}   

For some reason I could not use . Search (AID) with asNoTracking () but it's really nothing as I

Hope anyone with any problem with this Will help!

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -