none
How transactions will be implement inside my asp.net mvc web application that uses EF RRS feed

  • Question

  • I am using entity framework inside my asp.net mvc web application, but I can not understand how it will handle multiple transaction accessing the same data. For example I have the following action method that delete a collection and then loop through a collection and insert new entities:-

    [Authorize]
        public class SecurityRoleController : Controller
        {
    
            Repository repository = new Repository();
    //code goes here
    [HttpPost]
    public ActionResult AssignPermisionLevel2(ICollection<SecurityroleTypePermision> list, int id)
    {
        repository.DeleteSecurityroleTypePermisions(id);
        foreach (var c in list)
        {
            repository.InsertOrUpdateSecurityroleTypePermisions(c,User.Identity.Name);
        }
        repository.Save();
        return RedirectToAction("AssignPermisionLevel", new { id = id });
    }
    
    

    which will call the following repository methods :_

    public void DeleteSecurityroleTypePermisions(int securityroleID)
    {    
        var r = tms.SecurityroleTypePermisions.Where(a => a.SecurityRoleID == securityroleID);
        foreach (var c in r) {
            tms.SecurityroleTypePermisions.Remove(c);
        }
    
    }

    and also the following repository method:-

    public void InsertOrUpdateSecurityroleTypePermisions(SecurityroleTypePermision role, string username)
    {    
         var auditinfo = IntiateAdminAudit(tms.AuditActions.SingleOrDefault(a => a.Name.ToUpper() == "ASSIGN PERMISION").ID, tms.SecurityTaskTypes.SingleOrDefault(a => a.Name.ToUpper() == "SECURITY ROLE").ID, username, tms.SecurityRoles.SingleOrDefault(a=>a.SecurityRoleID == role.SecurityRoleID).Name, tms.PermisionLevels.SingleOrDefault(a=>a.ID== role.PermisionLevelID).Name +  " --> " + tms.TechnologyTypes.SingleOrDefault(a=>a.AssetTypeID == role.AssetTypeID).Name);
         tms.SecurityroleTypePermisions.Add(role);
         InsertOrUpdateAdminAudit(auditinfo);
    }
    So let say two users call the above action method at the same time; will their transactions conflict with each other? , or all the transaction actions (Deletion & Addition) will execute for the first transaction , and then the other transaction will start (which should get exception for deleting  null objects or inserting an already existing objects ) ?


    Tuesday, August 20, 2013 4:26 PM

Answers

  • Hi johnjohn123,

    I am sorry for misunderstanding you.

    So is it that you want to know transaction processing mechanism of Entity Framework?

    In my understanding, the Entity framework will create an internal transactions when we call the SaveChanges().

    Since I do not know the structure of your program, I assume it that the repository.Save() to be the SaveChanges() in your case.

    When we run our program and before we call SaveChanges(), Entity Framework will give all entites which are in the same DBContext a status(Add for new entity, Modify for entity modified and etc).

    For watching their status, we can use db.Entry<TEntity>( TEntityObject).State.ToString().It will show the current status of the  TEntityObject.

    If we giving up calling SaveChanges() at the end, all operations we did before as Add, Modify and Remove will be losed because there is not a transaction.

    And it is opposite if we call. New entites will be added to database and modified entites will be modified in database.

    So In my understanding, operation contains add and delete is in the same transaction in your case actually. When you call repository.Save(), the transaction creates and it will send all operations(contains add and delete)  to database.

    There is a link regarding transaction with Entity Framework:

    http://msdn.microsoft.com/en-us/library/vstudio/bb738523(v=vs.100).aspx.

    If I have misunderstand you, please let me know.

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 22, 2013 9:41 AM
    Moderator

All replies

  • Hello,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is that you want to know how the Entity Framework to deal concurrency issues.

    If I have misunderstood, please let me know.

    As far as I know that Entity Framework provides two ways to handle concurrency conflicts: laissez-faire and open concurrent processing.

    1. Laissez-Faire: Entity Framework dese anything, it is default.
    2. Open Concurrent Processing: Optimistic concurrency. Once a collision occurs, the system will capture the conflict, according to the policy automatically.

    There is a link that shows how to deal concurrency with the Entity Framework in an asp net MVC application:

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 21, 2013 10:14 AM
    Moderator
  • Hello,

    Welcome toMSDN Support Forum.

    From your description, I notice the issue you are experiencing is thatyou want to know how the Entity Framework to deal concurrency issues.

    If I have misunderstood, please let me know.

    As far as I know that Entity Framework provides two ways to handle concurrency conflicts: laissez-faire and open concurrent processing.

    1. Laissez-Faire: Entity Framework dese anything, it is default.
    2. Open Concurrent Processing: Optimistic concurrency. Once a collision occurs, the system will capture the conflict, according to the policy automatically.

    There is a link that shows how to deal concurrency with the Entity Framework in an asp net MVC application:

    http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

    I look forward to hearing from you.

    Best Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    I already know the time-stamp approach , for handling concurrency conflict when adding, deleting entities. but my question here is different . is how EF will handle transactions that contains (add, delete ) operation as i explained in my example ?
    Wednesday, August 21, 2013 11:10 AM
  • Hi johnjohn123,

    I am sorry for misunderstanding you.

    So is it that you want to know transaction processing mechanism of Entity Framework?

    In my understanding, the Entity framework will create an internal transactions when we call the SaveChanges().

    Since I do not know the structure of your program, I assume it that the repository.Save() to be the SaveChanges() in your case.

    When we run our program and before we call SaveChanges(), Entity Framework will give all entites which are in the same DBContext a status(Add for new entity, Modify for entity modified and etc).

    For watching their status, we can use db.Entry<TEntity>( TEntityObject).State.ToString().It will show the current status of the  TEntityObject.

    If we giving up calling SaveChanges() at the end, all operations we did before as Add, Modify and Remove will be losed because there is not a transaction.

    And it is opposite if we call. New entites will be added to database and modified entites will be modified in database.

    So In my understanding, operation contains add and delete is in the same transaction in your case actually. When you call repository.Save(), the transaction creates and it will send all operations(contains add and delete)  to database.

    There is a link regarding transaction with Entity Framework:

    http://msdn.microsoft.com/en-us/library/vstudio/bb738523(v=vs.100).aspx.

    If I have misunderstand you, please let me know.

    Thanks for your understanding.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, August 22, 2013 9:41 AM
    Moderator
  • I don't let EF control transaction processing. I use the System.Transaction.Scope and control it myself in code.
    Sunday, August 25, 2013 9:49 AM