Entity Framework transaction handling via SaveChanges

Possibly save 1 hour of your time: A common question that was asked is how to handle transaction within your application.
Fortunately, if you are using Entity Framework, it already does the heavy lifting work for you.
When you modify your entities in the application, it will track model and property changes behind the scene. And then when you call the following line of code, it will persists all updates to the database and resets change tracking in the object context.
objectContext.SaveChanges();

 

ObjectContext is the object that represents the database connection and its entities.

There may be situations when you may call this more than once in a request to create multiple transactions. I cannot think of a reason right now, but just remember that this acts like a commit transaction as each SaveChanges call will create a new transaction.

However, there are also cases where you may have more than one context to deal with. In that case you may pull data from one database and save them to another database. In this case, you may want to put both contexts’ SaveChanges calls in a TransactionScope to protect this as one atomic transaction.

References:

ObjectContext.SaveChanges Method ()

https://msdn.microsoft.com/en-us/library/bb336792(v=vs.110).aspx

LINQ and Entity Framework Errors

Possibly save 4 hours of your time: When working with LINQ and Entity Framework 6, you may encounter the following errors.

  1. Only parameterless constructors and initializers are supported in LINQ to Entities.
  2. The ‘Distinct’ operation cannot be applied to the collection ResultType of the specified argument. Parameter name: argument
    This happens on Union operation of two entities with different schemas
  3. An error occurred while preparing the command definition. See the inner exception for details.
    This happens on Union operation of Concat operation on two entities with different schemas
  4. System.InvalidOperationException : The specified cast from a materialized ‘System.Int32’ type to the ‘System.String’ type is not valid.
    This happens on Union operation of Concat operatoin on two entities with different schemas

First one is obvious, you must have parameterless constructor when selecting new object transformation.

from i in dbcontext.TableName

select new EntityObjectName() { ID = i.ID }

Second and Third one is similar. When you select new object with properties that are in array form, you may get these errors. One idea would be to simply change this to IEnumerable or IList.

Fourth, this one took me awhile to figure out. It basically happens when the property being transformed came from two different tables and have different data types on the database backend. A simple fix is to check for null and turn into empty string on both the union/concat select. This seems to fix the issue.

from i in dbcontext.TableName

select new EntityObjectName() { Value = i.Value ?? “” }

UNION or CONCAT

from j in dbcontext.TableName2

select new EntityObjectName() { Value = j.Value2 ?? “” }