Can’t get too far without Transactions support. Guess what? EF Core’s got your Back!!

A transaction symbolizes a unit of work performed within a database management system against a database, and treated in a coherent and reliable way independent of other transactions. A transaction generally represents any change in database.

If the transaction is committed, all of the operations are successfully applied to the database. If the transaction is rolled back, none of the operations are applied to the database.

The default behavior of a transaction is if the database provider supports transactions, all changes in a single call to SaveChanges() are materialized in a transaction. However, if any one of the changes fails, then the transaction is rolled back without any change(s) being materialized to the database. What this means is that SaveChanges() is guaranteed to either completely succeed, or leave the database in its original state in the event of a failure.

Let’s see this in action, but first, let’s see what we have in our database table called Departments before we call our method.


See the code snippet below. Notice SaveChanges() operation is being used twice. However, LINQ query is executed only once.

      private static void TransactionUpdateDelete()
            using (var db = new DepartmentContext())
                using (var trx = db.Database.BeginTransaction())
                    int depId = 15;
                    var depts = db.Departments.Find(depId);
                    depts.location = "New Delhi";

                    //Delete a record
                    db.Database.ExecuteSqlCommand("delete from dbo.Departments where depId = 16");

                    var deptList = db.Departments.ToList();

                    foreach (var item in deptList)
                        Console.WriteLine(item.depName, item.depName, item.location);

Snapshot of Data View After the Commit(). Notice DepartmentID 15 is updated from New York to New Delhi and DepartmentID 16 is yanked out as a result of the Deletion


And lastly, the output window to demonstrate the successful execution of the two operations, namely, Update and Delete within the context of a Transaction in EF Core.


Posted in ORM | Comments Off on Can’t get too far without Transactions support. Guess what? EF Core’s got your Back!!

I don’t see Visual Tooling in EF Core. What are my options?

Folks, some of the cool features/workflows that you are used to using in EF 6.x and with some of the prior versions such as Entity First, Database First that provide visual tooling are a thing of the past in EF Core.

Don’t despair; there’s hope. There are some third-part vendors that make tools which can be easily incorporated in your EF Core projects.

Some of the names of these companies are the following:
LLBLGen Pro: It is an entity modeling solution with support for Entity Framework and Entity Framework Core. It lets you easily define your entity model and map it to your database, using database first or model first, so you can get started writing queries right away. Apart from being one of the leading entity modeling solutions for .NET, it can be used with Entity Framework, NHibernate, Linq to SQL and our own ORM, the LLBLGen Pro Runtime Framework (included). It lets you easily define your entity model and map it to your database so you can get started writing queries right away.


Telerik Data Access: If you’ve been a .NET developer, the odds are you’ve probably used at least one of the Telerik products, may it for the good ‘ol battle ship grey forms,  ASP.NET Web Forms, ASP.NET MVC, Mobile, Just Code, Fiddler etc.


Telerik’s Data Access tools solve development problems with the most powerful data access framework. Some of the key features of the Data Access tool from Telerik bring you the following:

  • Mapping and Model Creation including the mapping of Stored Procedures & Functions
  • Change Tracking and CRUD Operations
  • Powerful Runtime Capabilities
  • Performance Optimization
  • Support for Multiple Databases
  • Unique Fluent API Capabilities
  • Data Access Profiler
  • Testability

More information can be obtained here.

DevArt: DevArt is another one of the ORM solution providers. It offers wide set of the .NET database connectivity tools with ORM support. It includes the following ORM tools and solutions:

  • Entity Developer
  • dotConnect
  • LINQ
  • LINQ Connect

DevArtMore information can be obtained here.

Posted in ORM | Comments Off on I don’t see Visual Tooling in EF Core. What are my options?

What does Connection Resiliency offer when using EF Core!

Connection Resiliency comes handy when you have a failed database operation. This operation could be the result of moving a database from one server to another for the purposes of load balancing, thus causing the database to become unavailable for a short period of time even if its for a few seconds.

Another scenario could be that you have deployed a database as a container and for some reason the container is not ready for other container(s) or micro-services etc.

In essence, Connection resiliency retries failed attempts in the event of database connection failure and is specific to relational databases. This feature was recently introduced as part of EF Core’s 1.1 release.

It’s implementation is fairly straight-forward. You need to define the execution strategy  by overriding the OnConfiguring() method in your dbContext or in Startup.cs if using an ASP.NET Core application.

Note the number 5 that I am passing to the EnableRetryOnFailure(5) which denotes of retries if the database commands were to fail. This method has two overloads which in addition to the number of retries, you can also pass in the delay between retries in milliseconds.

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            optionsBuilder.UseSqlServer(@"server=obi-oberoi; database=Demo.Department; Trusted_Connection=True;",
                options => options.EnableRetryOnFailure(5));

Posted in ORM | Comments Off on What does Connection Resiliency offer when using EF Core!

What does EF Core give me out of the box?

As you might know, EF Core is brand new and it has long ways to go before reaching parity with EF 6.x, although some of the features that were murky in EF 6.x will probably never be added to EF Core.

That being said, it has some very good features that not only make the transition from EF 6.x to EF Core smoother, it’s also a lot of fun learning some of goodness of this otherwise new shiny tool.

Make no bones about it. EF Core is production ready and there are a lot features that you can already take advantage of. For example:

  • It’s constantly evolving and Microsoft is betting on it for its success going forward
  • It has the familiar APIs that we are used to like DbContext, DBSet etc.
  • It has full .NET Support in addition to being cross platform (can run on iOS, Linux, Android)
  • It is highly composable and hence lightweight
  • It is built from Ground Up and has APIs that highly performant
  • It supports Relational Databases and will have support for NoSQL databases in the future
  • The entire EF Core stack is open sourced on Github
Posted in ORM | Comments Off on What does EF Core give me out of the box?

Implementing Cascade Delete behavior in EF Core

The definition of Cascade Delete is if a record in the parent table is deleted, the corresponding records in the child table will automatically be deleted.

Now that we know what Cascade Delete is all about, let’s implement this behavior in EF Core where we will use Department and Employee tables. A Department can have one or more Employees. In other words, it’s a One to Many relationship. The idea is when we delete a record in the Department table, any references it has to the deptId key in the Employees table will also be deleted.

In the code that follows, the code loads records from the Department table and its corresponding Employees from the database. Notice the include method that is being used here. When SaveChanges is called, the cascade delete rules will be applied to the entities that are being tracked by the context.

Before Deletion:


private static void CascadeDelete()
            using (var db = new DepartmentContext())
                var depts = db.Departments.Include(e => e.Employees).First();

After Deletion:

CascadeDeleteAfterAs you can see DepID 8 which happened to be the first record in the Department table had two related Employees, namely, Rebecca D’Souza and Chris Wood. When the Department record with an Id of 8 was removed, it also deleted its corresponding Employees.

Since all Employees were tracked by the dbContext, the cascade behavior was applied before saving entries to the database. As a result, Entity Framework issued a DELETE statement for each of the matching entities.

Posted in ORM | Comments Off on Implementing Cascade Delete behavior in EF Core