Techbubbles

Entity framework 7 features overview

What is Entity Framework 7?

Entity framework 7 is a lightweight and extensible version of Entity Framework. This post outlines some basic concepts in EntityFramework 7 and then what is new in EF7?

New platforms targeted with EF7 like windows Phone and Windows Store. Off course ASP.NET 5 is supported. The core of entity framework is now supported in mac and Linux. Historically EF 7 is supporting Relational databases but now it also supports non-relational databases. Example providers that Entity framework 7 supports are

  • SQL Server
  • SQLite
  • Azure Table Storage
  • Redis
  • In Memory (for unit testing)

Basics of Entity Framework 7

image

As part of the default ASP.NET 5 project template you have got Entity Framework installed. Note: EntityFramework.SqlServer is installed as separated package. Create a simple Bike model in your project as shown in the following picture

image

Create a CycleSalesContext class which inherits from DbContext class. It exposes DbSet for each type in the model for querying the instances of the type

image

In EF 7 the code base configuration is much easier compare to EF6. In EF7 you can override OnConfiguring method.

Protected override void OnConfiguring (DbContext options)

{

    Options.UseSqlServer(connectionstring);

}

This is all you need to do to get your model up and running. However in EF7 it would not do the magic database creation. Now create the database based on the model using k commands and package manager

Type K ef at the command prompt then you will see the following help screen

image

image

ef mentioned in the command because it is listed under commands section in project.json file

image

Ef can be replaced with anything like foo etc

To add a new migration to the project the k command is

clip_image002

When you execute the above command it will then create the migrations folder in your project.

K ef migration apply which completes the db creation process.

In startup.cs file, CinfgureServices method you will see the following code related to scaffolding

image

You can use extension methods like AddDbContext to inject DbContext for other controllers who depend on it.

To generate a controller using a model and DbContext then use the following K command

image

Exclusively in ASP.NET 5 EF7 added some help pages to know about the migrations. For example if you introduce a model change which obviously requires database change. If you refresh your application page without adding and applying a new migration then you will see a nice help page

image

This context based message in help page is useful during the development and you can restrict this not to flow to the production environment. You can restrict this by writing the following code in Configure method in startup.cs file.

image

Batching improvements in EntityFramework 7 – executing multiple sql statements, example multiple updates can send as one batch (one round trip) to SQL Server.

image

EF7 can generate sql statements when you do the CRUD operations from user interface.

Unit testing is made much easier in EntityFramework 7. The sample test looks as below

var options = new DbContextOptions<CycleSalesContext>()
                    .UseInMemoryStore();

You can then pass the options object to add some data and to assert the results.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
CodePattern.net Recent comment authors
  Subscribe  
Notify of
CodePattern.net
Guest

Unit test easiness is noteworthy in this edition of Entity Framework. 🙂