c# - Passing object from my Controller action method to my Repository method -


I am working on an asp.net mvc 5 web project and I have a repository Model class is created, and I am calling my repository with my verb method. For example, in my post editing methodology, I have the following code: -

  repository repository = new repository () ; [Http: post] [validAntiFergertin] edit public action result (security roll roll) {try (modelstate.ISIAGID) {repository.InstituteOrUpdateRole (role); repository.Save (); Return Redirect Action ("index"); }}}  

and there are repository model classes: -

  public class repository {Private teamTTS = new teammate}; Public Zero InsertOrUpdateRole (Security Role role) {role.Name = role.Name.Trim (); if (Role.SecurityRoleID == default (int)) // // New unit // code goes here {T. Entry (role) .set = EntityState.Modified;}}  

My question arises when I pass the object from my verb to my repository, how the entity framework will behave; will it make another copy in the repository, or will it keep track of the same object? Entity Framework on the same object Will work within the repository system within and in session, then how does the object framework track the object?

The second question , what is the best way to follow; To bring the whole object from the action method to the repository (as I am currently doing), or instead to pass the object ID only, and to bring the object ID back into the repository model, something Follow in: -

  repository.InsertOrUpdateRole (Role.RoleID);  

and inside the repository

  Public Zero InsertOrUpdateRole (int id) {var SecurityRole = t.SecurityRoles.SingleOrDefault (a => a.SecurityRoleID = = Role.SecurityId); }  

C # objects have been passed from context. When you save your role to your ActionResult , you notice that id is found after the InsertOrUpdateRole call has gone. EF is starting to track changes as soon as some context is linked. So your first approach works fine (I also use it) to make sure that the model is passed with all the values ​​set with the InsertOrUpdateRole function, the EF will overwrite the entire record So if you forget any asset in your view it will become a blank value in the database. If you just want to update some properties of the model, then use your second method.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -