Why is EF Core so blazing fast than its Predesessor?

It’s worth noting that EF Core is a brand new shiny tool from Microsoft that has been built from the ground up. It is lightweight, extensible and it comes with a plethora of new features when compared with EF 6.x.

Now, when it comes to performance, in just about all possible scenarios where tests have been conducted on CRUD related operations, EF Core has invariably outperformed EF 6.x. This is despite EF 6.x code having been tweaked for maximum optimization against somewhat naïve EF Core code, and yet coming out as a winner. So this begs the question…why is EF Core so much faster?

Well, the answer lies in the following:

  • Caching
  • Batching
  • Faster Initialization to build object models (conceptual model, mappings, storage etc.)

Stay tuned for more!!

Posted in Uncategorized | Comments Off on Why is EF Core so blazing fast than its Predesessor?

EF 6.x or EF Core? I am confused!!!

If you are an enterprise developer or you are building a Line of Business Application and targeting the full .NET Framework and who also cares about visual tooling, EF 6.x is the way to go. It also means you can do WPF, Win Forms, ASP.NET MVC, Console apps etc.

EF 6.x is a mature product that is built over a period of time starting from 2008 till now. During this period, we have seen continuous improvements by way to adding new features, bug fixing etc. It has a rich eco-system of database providers and provides at least four workflows to build a model and database. It includes the following:

  • Code First
  • Code First From Database
  • Database First (EDMX Designer)
  • Entity First (Visual EDMX Designer)

EF Core on the other hand can target a multitude of application models ranging from full .NET Framework, ASP.NET Core as well as Xamarin. So if your dominant concern is to deploy your application on devices such as iOS, Android and such, EF Core is probably the way to go!

Apart from being cross-platform, EF Core is also a lightweight version of Entity Framework and is therefore built on a more leaner and faster development stack. Its because it has been built from ground up and is built over a set of services which make up the core of .NET.

Not only new platforms and new devices, EF Core has been architected to target new data stores. These data stores can be both relational and non-relational. Some of the examples of database providers are the following:

  • Azure Table Storage
  • Reds
  • In Memory (functional testing)
  • Relational providers such as:
    • SQL Server
    • SQLite
    • SQL Compact
    • Postgres
    • Oracle (prototyped)
    • DB2

more to come…

Posted in Uncategorized | Comments Off on EF 6.x or EF Core? I am confused!!!

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.

BeforeUpdateDelete

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");
                    db.SaveChanges();

                    var deptList = db.Departments.ToList();

                    foreach (var item in deptList)
                    {
                        Console.WriteLine(item.depName, item.depName, item.location);
                    }
                    Console.ReadLine();
                    trx.Commit();
                }
            }
        }

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

AfterUpdateDelete

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.

Output

Posted in Uncategorized | 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.

LLBLGenPro

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

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 Uncategorized | 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 Uncategorized | Comments Off on What does Connection Resiliency offer when using EF Core!