Ensuring Atomicity with Entity Framework - entity-framework-4

I am writing inserts and updates into a database using Entity Framework. The issue is that I want to do what I used to do with stored procedures, which is make groups of updates and inserts atomic by putting transactions around them. How is this sort of thing possible with linq to Entities/Entity Framework?
Thanks,
Sachin

When you call SaveChanges it automatically executes everything in single transaction. If you need to group changes into separate transaction you must do them in sequence and call SaveChanges for each group separately.

Related

EF 6 DB First Audit Logs

I have a EF 6 DB first MVC 5 application. My requirement is to do audit logging of every operation (including read). I went through many posts and have few queries:
Should audit logging be done at EF level (by overriding SaveChanges) or DB level (by using triggers). Which is the recommended way.
I want to log one row per entity change instead of per property change. What am I thinking is to make a valid XML schema but then each entity will have different schema depending on the column. Any other inputs on how to achieve this
I want log for read operation too
Last thing is, client wants to maintain checksum value per row using SHA3 or MD5.
Considering above points, what is the suggested approach. I could really use some pointers.
To achieve this, I did not use any utility as my requirements were bit different. Finally I went ahead with overriding the SaveChanges method of DbContext used by EF. Also used Newtonsoft JSON library to convert whole updated object to JSON and save it.
To get complete code, check this link - How to audit MVC app which used EF DB first approach

How to modify CRUD operations with Entity Framework in .NET MVC application

I need to work out how to do CRUD stuff in an MVC application, passed to me by a former colleague (I don't have any other info, just the application and the database). So I can see there is Model1.edmx and the model browser that contains MyApp.Model>EntityTypes>MyTable representation, and MyAppModel.Store that contains a representation of the table, and the Model1.Designer.cs file which has methods that look like they must be CRUD related (e.g. OnLastNameChanging, OnLastNameChanged). I can run the application and insert and update records to a db table.
What I need to know is where / how do I code other CRUD operations and use Entity Framework to work with WHERE clauses, and do stuff like update another record in a table depending on the value of a given field in the record being inserted or updated.
I've worked with MVC on one other small project but haven't really worked with entities. I'm used to the WebForms / ADO.NET / stored procedures way of doing things.
Any help gratefully received.
Happy new year!
I guess you are using database first approach.
some useful links for Code first approach:
http://www.codeproject.com/Tips/793159/Code-First-Approach-in-Entity-Framework-using-Data
other for database first approach
http://www.aspdotnet-pools.com/2014/08/insert-update-delete-operation-in.html
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

Entity split in two databases

I'm working on a project already started by several developers before me. One thing in particular bothers me is that they have single entity split in two databases.
Entity is called Tracker.
First database is called ConfigBase, and it has table named Trackers that has TrackerId along with it's attributes.
Second database is called StoreBase, and it also has table named Trackers, whose elements have matching TrackerId as it is in the first base.
Moreover, to have things even more complicated, when you access specific tracker in ConfigBase, you gain SQL server name and credentials that allow you to access it in StoreBase.
Now all this isn't too much complicated if you use plain old ADO.NET. But as my task is to raise entire solution to newest EF 4.3.1, I'm having troubles maintaining consistency of my entity. Half of things related to Tracker entity are in ConfigBase and the other half in StoreBase, and usually I have to get both to get some result.
Is there any solution to this that does not involve virtual merge on database level. I'm looking for a solution that can be done with Code First modelling.
Thanks in advance!
No there is no solution provided out of the box because EF itself is even not able to use more than one database per context. So you will either merge your databases or you will access each database separately (with separate Tracker entity per database) and merge data somehow in your application.

Running 3 Insert methods as transaction

I have a repository where I have common methods for adding etc.
And I would like to run 3 methods as a transaction. Is that possible if each of them have "SaveChanges()" in them?
e.g
_somerep.AddCamel("test");
_somerep.AddGoo("test");
_somerep.AddGopher("test");
/Lasse
As long as the repository uses a single entity context and all insertions are handled by a single call to context.SaveChanges(), they are automatically enrolled in a transaction by Entity Framework.
You can also manage transactions via TransactionScope as mentioned here (but I wouldn't suggest it since it requires Microsoft Distributed Transaction Coordinator to be installed...and can cause some weird issues):
How to: Manage Transactions in the Entity Framework

Working with Sets of Self-Tracking Entities causing performance issues

I saw this Post by ADO.Net team which looks very promising until I started using it in my application. I have EF 4.0 model with close to 100 self tracking entities. After including the iterator in my project, any of the extension methods "StartTrackingAll" or "StopTrackingAll" would take 5sec to finish. Has anyone ran into same issue or anyone knows of any better option.
Are your entities in relation? In that case you don't need to use StartTrackingAll because StartTracking itself starts tracking for whole object graph:
The StartTracking method instructs the
change tracker on the entity to start
recording any changes applied to the
entity. This includes changes to
scalar properties, collections, and
references to other entities. The
self-tracking entities start tracking
automatically when they are
deserialized into the client through
the Windows Communication Foundation
(WCF). The tracking is also turned on
for newly created entities in the
following scenarios:
* A relationship is created between the new entity and an entity that is already tracking changes.
* The MarkAs[State] or AcceptChanges method is called on an entity.
If you are not using related entities it sounds strange that you need to track 100 entities in the same time. Also if entities don't have relations it is perhaps not needed to track them at all.

Resources