EF Core – What is it?

I have been meaning to write something on EF Core, a topic that is near and dear to my heart.

Microsoft has done phenomenal work in shaping this ORM what has turned out to be a real hot product, although the best is yet to come! In doing so, they have left no stone unturned in promoting EF Core in major conferences across the globe. This aggressive approach to this otherwise new technology has clearly resonated loud and clear amongst the developer community in particular and corporates in general. As a result, EF Core has already begun to gain traction.

Make no bones about it…EF Core is new and it’s still a version 1.0 product. Therefore, it wouldn’t be fair to compare it with its predecessor that has gone through numerous iterations for over eight years now.

So what exactly is EF Core (formerly EF7) and how is it different from Entity Framework?In Microsoft’s own words, Entity Framework (EF) Core is a lightweight and extensible version of the popular Entity Framework data access technology. EF Core is an object-relational mapper (ORM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write.

The most current release of Entity Framework i.e. 6.1.3 is by far the most stable product we have seen yet! It has gone through eight plus years of development, bug-fixes and constant improvement thus providing a rich eco-system of data providers. These database providers are predominantly relational. Having said that there should be no doubt that Entity Framework is mature which offers a rich set of features such as Creating Model using Spatial data types, Many-to-many relationships, Visualizing a model, Alternating inheritance mapping patterns to Lazy Loading, Saving Data and Database Schema Management just to name a few!!

EF 6.x to EF Core is “Port” not an “Upgrade”

What it means is that there is no upsize wizard that can magically update all your Entity Framework project(s) to EF Core. Furthermore, EF Core does not support the EDMX file format for models. In order to port your model, your best bet is to reverse-engineer your model from the database for your application.

While EF Core is built with a lot of the same API’s, their implementation is totally different. For example, some of the common top level API’s such as DbContext, DbSet are still the same which in my opinion is a wonderful thing. Having said that, these top level API’s which are also part of EF Core are highly optimized and are guaranteed to give a significant boost when performing CRUD operations against a data store.

EF Core is made up of core components which in essence are a collection of loosely coupled services. There are categories of services ranging from Context, Provider specific, Design-time and User services.

Database Providers

Another important aspect of EF Core is that it can target a slew of data stores irrespective of whether they are relational or non-relational. Unlike Entity Framework which can only target relational databases namely, SQL Server, Oracle etc. EF Core can target these, but also target non-relational ones such Redis, In-Memory (for Testing) etc.

Which One is Right for You

Don’t be afraid to use EF 6.x if you are building an enterprise app. EF 6.x is tried and tested technology with many years of features, bug fixes and stabilization. However, if you are building an application that is cross-platform or if it targets ASP.NET core, then EF Core is definitely the right choice. The diagram below gives illustrates the platforms and App Models that EF 6.x and EF Core support.


Posted in Uncategorized | Comments Off on EF Core – What is it?

MVPMIX 2016 Toronto Technology Conference


Due to popular demand, we are bringing MVP MIX 2016 to Toronto yet again!!

Seats are limited, so reserve your spot now. There will be two simultaneous tracks with sessions ranging from .NET, VS Code, ASP.NET Core, EF Core, JavaScript frameworks, Roslyn, Open Source, Cross-plat Apps with Xamarin and Azure, Document DB, Keno UI, Office 365, NuGet and more!!

We have a stellar line-up speakers who will enthrall you with their technical prowess and virtuosity.


MVPMIX2016Speakers1See you on October 17th at the Canadian Microsoft’s Toronto Headquarters.


Posted in Uncategorized | Comments Off on MVPMIX 2016 Toronto Technology Conference

How to use a single database connection with multiple Database Contexts!!

Reading is to the Mind what Bounded Context is to DDD!!

Typically, for small to medium-sized projects, you probably have a single database context or two, but what if you are working on a large project that requires multiple DbContexts!

As you create the so-called ‘bounded’ contexts (a pattern that was coined by Eric Evans in his ground-breaking book on Domain Driven Design), you need to specify a connection string. In other words, tables that are created in the database are represented as entities in the domain irrespective of the methodology used to create the domain.

A domain is made up classes which are wrapped in a single database context. If you have hundreds of tables and a single domain, then, from the maintenance standpoint, this can potentially be less maintainable. It is therefore a good practice to group these classes in multiple domains or multiple bounded-contexts.

The code below shows one of the context classes which inherits from the base DbContext class.

ContextsNotice the three database contexts in Solution Explorer that we have in the project, each of which is pointing to the same database defined separately in App.Config like so:AppConfig

Ideally, what we want is to use a single connection to the database.

In order to do that, let’s create  a new class and call it BaseContext. The BaseContext is defined as a generic class with a constraint where you are required to pass in value of DbContext.

So instead of having multiple DbContext(s) and having an individual connection string for each of them, we’d rather have a single connection string and use it for different DbContexts like so!BaseContext

Note that BaseContext constructor is marked static. This declaration ensures that the Database Initializer setting is set per an application instance of the given constructor.

In order to do that, modify each one of the dbContext classes to inherit from the BaseContext instead of inheriting from DbContext directly like before. Furthermore, in app.config or web.config, remove additional references to the database and just keep the one as shown below:AppConfig1




Now, when you run the application, EF would produce the same output pointing to a single database albeit multiple contexts through the magic of a generic BaseContext.

If you want to know more about DbContext, I would highly recommend a book by Julie Lerman, titled, Programming Entity Framework: DbContext.

Happy Coding!

Posted in Uncategorized | Comments Off on How to use a single database connection with multiple Database Contexts!!

Mapping Stored Procedure(s) to Entity Framework

You have written a nice stored procedure and you also use Entity Framework periodically. You may have also used LINQ to SQL in your past life, but now you want to use Stored Procedure with LINQ to Entity instead. So what do you do?

Well, in this short but succinct tutorial, we’ll do just that!

First, let’s write a simple stored procedure. You can create one in Visual Studio if you don’t have SQL Express or SQL Server Management Studio.


Let’s fire-up Visual Studio and do the following:

  1. Create a Console app
  2. Add reference to the latest version of EF. As of this writing I am using 6.1.3
  3. Add a new ADO.NET item from the template and select “EF Designer from database” option. See image below:EntityDataModel
  4. In the next step,  you’ll have an opportunity to select the database objects i.e. Tables, View, Stored Procedures. Here, we will select a couple of tables and stored procedures. See image below:
  5. After you hit Finish, Entity Framework will generate all the necessary goo including an diagram, designer and TT classes.
  6. Open the diagram. As you can, it’s pretty straight forward.Diagram2
  7. Next right-click on the Department Entity and select ‘Table Mapping’ from the context menu.
  8. Next, go to the Program file, new up the context and invoke the stored procedure, in this case ‘GetAllDepartments() and write the information out to a console

In the future post, we will do a little deep dive and show you how to map complex stored procedures to Entity Framework.

So stay tuned!!!

Posted in Uncategorized | Comments Off on Mapping Stored Procedure(s) to Entity Framework

Using Stored Procedures from LINQ to Entity

Entity Framework 6.0 and above supports the use of Stored Procedures if you choose EF Designer from Database workflow or  Generate from database (depending upon the version of VS.NET) option which essentially allows you to create a model that is reverse engineered from an existing database. After EF finishes reading the database schema, it will display a list of database objects including tables, views and stored procedures. See image below:


However, if you choose Code First from Database workflow, the option to import Stored Procedures from database does not exist. This is by design. See image below:


Now, what if you use the above workflow to reverse engineer your database to a model and still want to use stored procedures. How would you use it?

There is a SqlQuery method which is part of the Database object, i.e. Database.SqlQuery. This method is used to for creating a raw SQL query to return elements of a given type. The ‘type’ can by any type that has properties that match the names of the columns that are returned from the query.

As with any API that accepts SQL, it is important to parameterize any user input against a SQL injection attack. For that you can use parameter place holders in the SQL query or a Stored Procedure and supply value(s) as additional argument(s) like so:SqlQueryMethod

Call the method and pass a value which in this case happens to be an employeeID of 1.



So this was a short tutorial on calling a stored procedure leveraging SqlQuery method using LINQ to Entity.


Posted in Uncategorized | Comments Off on Using Stored Procedures from LINQ to Entity