Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Implementing transaction in MVC as Attribute

Implementing transaction in MVC as Attribute

Scheduled Pinned Locked Moved C#
asp-netcomarchitecture
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nitin_ion
    wrote on last edited by
    #1

    Hi Can i implement transaction in MVC using EDMX like this link [Handling Transactions]

    D 1 Reply Last reply
    0
    • N nitin_ion

      Hi Can i implement transaction in MVC using EDMX like this link [Handling Transactions]

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You could, but EF already wraps every batch of updates (when you call DbContext.SaveChanges) in a transaction by itself. You get more control over this process if you're using EF6.0 or above. Read this[^] for more information. Before you get into doing your own transactions you might want to read this[^].

      A guide to posting questions on CodeProject

      How to debug small programs
      Dave Kreskowiak

      N 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You could, but EF already wraps every batch of updates (when you call DbContext.SaveChanges) in a transaction by itself. You get more control over this process if you're using EF6.0 or above. Read this[^] for more information. Before you get into doing your own transactions you might want to read this[^].

        A guide to posting questions on CodeProject

        How to debug small programs
        Dave Kreskowiak

        N Offline
        N Offline
        nitin_ion
        wrote on last edited by
        #3

        i know this but problem with this is mostly these calls are in DAL. IN General you call ur business function from controller and there are multiple calls made which should be in transaction e.g. using(Transaction.....) { BLL.SomeFunction1() BLL.SomeFunction2() }

        D 1 Reply Last reply
        0
        • N nitin_ion

          i know this but problem with this is mostly these calls are in DAL. IN General you call ur business function from controller and there are multiple calls made which should be in transaction e.g. using(Transaction.....) { BLL.SomeFunction1() BLL.SomeFunction2() }

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          OK, that's still a bit backwards. The controller should be concerned with translating between the View (ViewModel) and the Business Logic, not controlling a transaction which is something that belongs in the Data Layer and possibly controlled by the Business Logic. So long as the last call you make in the Business Logic is the DbContext.SaveChanges, all the changes the Business Logic makes will be wrapped in a Transaction AUTOMATICALLY. You don't have to create a transaction yourself!

          public class BusinessLogic
          {
              private MyDbContext context = new MyDbContext();
          
              public void BusinessFunction1()
              {
                  // Don't call SaveChanges in here.
              }
          
              public void BusinessFunction2()
              {
                  // Don't call SaveChanges in here.
              }
          
              public void SaveChanges()
              {
                  // All changes made above will automatically be wrapped
                  // in a transaction by EF.
                  context.SaveChanges();
              }
          
              public void Dispose()
              {
                  context.Dispose();
              }
          }
          

          Besides, you already linked to the article that shows you how to do it! I already linked to a couple of articles that shows you the pitfalls of what you want to do. The problem with what you want to do is that you have to be VERY careful that the transaction mode you use doesn't LOCK THE ENTIRE TABLE while you do your transaction processing. If you're not careful, the lock will prevent other instances from even reading the data in locked tables, preventing other users of your web site from seeing pages that are backed by data!

          A guide to posting questions on CodeProject

          How to debug small programs
          Dave Kreskowiak

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups