Entity Framework 50 first steps and impressions
I’m mostly a NHibernate user, but EF code first approach is intriguing, especially in situation where the customer prefer to use only Microsoft Stack libraries , thus EF 5.0 is a good approach to write quickly an Efficient data access module in really few clicks. Enabling EF5 on your own project is as simple as adding a reference with Nuget, go to package manager and type install-package EntityFramework
and the game is done. Now you can start writing a simple class that will represent a table in the database.
|
|
Now you need a class that inherits from DbContext to access the database and to expose all the classes you want to be saved/loaded from your database.
|
|
Now you should add a connection string to Sql server database you want to use, this is really simple because by convention the context you created looks for a connection string in configuration file named exactly as the class name, in this example SampleContext
|
|
Actually I’ve no database called Samples in my local server so I wanted to create one with the correct structure to save my simple Customer class, but I can create automatically a database with the right schema to contain my data thanks to EF database-migrations. To enable migrations you should simple type Enable-Migrations on package manager console
PM> Enable-Migrations
Checking if the context targets an existing database…
Code First Migrations enabled for project FirstSteps.
Now you should have another folder called Migrations with a Configuration.cs files in your project, you can edit this file changing the value of AutomaticMigrationEnabled from false to true to enable automatic migrations. Now you can issue Update-Database command in your package manager console.
PM> update-database
Specify the ‘-Verbose’ flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Applying automatic migration: 201209150928099_AutomaticMigration.
Running Seed method.
Et voilà you have your database generated and ready to use. As you can see from Figure 1, the convention used is to create a table with the very same name of the DbSet properties exposed by your data context and a column for each property of the class.
Figure 1: Newly generated database with database-migration of EF5
Everything is created by convention over configuration, as an example if you have a property called Id, corresponding table column will be the primary key for the table , and if the value is an integer automatically the column is generated with Identity Set to true.
Figure 2: Id column was generated with Identity specification equal to true
Now you can access database simply creating an instance of SampleContext (do not forget to dispose it when you finished using it) and thanks to LINQ provider you can issue query on it, because all DbSet properties you expose are IQueryable so you can use to simply access all Customers with a simple Foreach.
|
|
With really few lines of code you have created entity classes, corresponding database and can write/load data on it.
Alk.