Implementing transaction in MVC as Attribute
-
Hi Can i implement transaction in MVC using EDMX like this link [Handling Transactions]
-
Hi Can i implement transaction in MVC using EDMX like this link [Handling Transactions]
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 -
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 Kreskowiaki 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() }
-
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() }
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