The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Entity Framework 5 – An Introduction

Apr 8

Written by:
2013/04/08 12:38 PM  RssIcon

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.

  1. 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.
  2. 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.
  3. 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 { getset; }

        public string Name { getset; }

        public string CatalogDescription { getset; }

        public System.Guid rowguid { getset; }

        public System.DateTime ModifiedDate { getset; }

      

        public virtual ICollection<Product> Products { getset; }

        public virtual ICollection<ProductModelProductDescription
												

            ProductModelProductDescriptions { getset; }

    }

 

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

Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
  Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
  Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
  Blog Roll
Minimize
Print  
 
Categories
  Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
  Archive
Minimize
Archive
<May 2024>
SunMonTueWedThuFriSat
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
  Tags
Minimize
Print  
 
Contact Us Now
  Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print