Entity Framework 5 – An Introduction
Apr8Written by:
2013/04/08 12:38 PM
What is Entity Framework? A question that many developers have asked. Basically Entity Framework is an ORM (Object Relational Mapping) tool introduced in .NET 3.5. Entity Framework 5 (EF5) is the latest public release of Microsoft's ADO.net Entity Framework model. As of this writing, Entity Framework 6 is the next major release and is currently in Beta. You can see the Entity Framework Roadmap on CodePlex.
Entity Framework is the Microsoft preferred method of data access for .NET applications. The Entity Framework allows you to develop applications that utilize a model designed to sit between the actual data storage schema and the user interface within an ASP.NET application. Database generally store and access data in a relational way and have no concept of a data model or objects. Also, the database is not type safe, which can play havoc in a type save programming environment. As a result, Entity Framework also allows developers to program against a conceptual model that reflects application logic rather than a relational model that reflects the database structure.
Different Workflows
With Entity Framework there are different workflows, actually 3, you can follow to actually create you model.
- Database First: This workflow is perfect if you already have a database or prefer using another tool to create your database. Database first takes your existing database and reverse engineers it to create the classes (entities) and mappings. This model maps all database tables, views, stored procedures and column into classes, methods and properties.
- Model First: This is perfect when you do not have a database and also prefer to work in a visual environment when designing your model and database. Also, if you are not using another data modelling too. Once done, you can generate code to create the database and all its tables as well as generating the entity or classes to be used in code.
- Code First: This is perfect when you are in total control of all facets of the development cycle, including developing the database. It is also perfect if you prefer to get down and dirty and don't mind writing a few lines of code. Here you manually define your classes, relationships and mappings in code and later generate a database to use.
For this article I will be using a Database First approach. For two main reasons, one; I am mostly involved with large corporations which already have a production database in place or they have dedicated database teams building the perfect database, and second, I like working in a visual environment. Less code for me the better.
Building the Database First Model
Begin by adding an Entity Framework Model to an existing project. Select ad new item then select ADO.Net Entity model from the Data Templates column.
Next select Generate from database after clicking add. Here you can also go the model first route by selecting the Empty model option.
Next choose you data connection by either using an existing connection or by creating a new connection. Creating a new connection should be familiar to you if you have ever worked with databases before.
By select a new connection you can change the connection properties as per normal using the connection properties dialog.
Once that is done you are now left with selecting the entities (tables and views) and stored procedures and Functions to using in your application. Expanding each section will allow you to select or deselect each object under Tables, Views and Stored Procedures and Functions.
Once finished a model will be generated and you will be presented with a Entity Diagram.
Querying Entity Framework Using Linq
Now that you have a model in place you can query the model and work with data using Link, or more specifically Linq to Entities. Below is what the ProductDescription class might look like based on what we have done above.
public partial class ProductModel
{
public ProductModel()
{
this.Products = new HashSet<Product>();
this.ProductModelProductDescriptions =
new HashSet<ProductModelProductDescription>();
}
public int ProductModelID { get; set; }
public string Name { get; set; }
public string CatalogDescription { get; set; }
public System.Guid rowguid { get; set; }
public System.DateTime ModifiedDate { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<ProductModelProductDescription
ProductModelProductDescriptions { get; set; }
}
Now using a simple linq based query we can pull through the data. Notice that my code and model has no idea of the database structure. This is because I am coding against the Model, aka the DbContext.
using (EFDemoEntities dbc = new EFDemoEntities())
{
var prodModels = from p in dbc.ProductModels
select p;
}
Related Reading:
The Difference between IQueryable and IEnumerable
blog comments powered by