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
- Azure Table Storage
- In Memory (for unit testing)
Basics of Entity Framework 7
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
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
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)
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
ef mentioned in the command because it is listed under commands section in project.json file
Ef can be replaced with anything like foo etc
To add a new migration to the project the k command is
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
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
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
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.
Batching improvements in EntityFramework 7 – executing multiple sql statements, example multiple updates can send as one batch (one round trip) to SQL Server.
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>()
You can then pass the options object to add some data and to assert the results.