Feeds:
Posts
Comments

Posts Tagged ‘Entity Framework’

New features in EF 4.0 : http://blogs.msdn.com/somasegar/archive/2010/01/11/entity-framework-in-net-4.aspx

Channel 9 SharePoint 2010 Development videos: http://channel9.msdn.com/learn/courses/SharePoint2010Developer/

Read Full Post »

This tutorial will very quickly walk through creating a VERY SIMPLE  Entity Model. This is a VERY simple tutorial to simply get a taste of EF.

You will need visual Studio 2008 SP1 or later and SQL Server for the database and we will use the Nrothwind sample database.

ScreenHunter_01 Mar. 19 00.09

Open Visual Studio 2008:

ScreenHunter_02 Mar. 19 00.09

Create a new Project: (Make sure you are selecting Framework version 3.5)

ScreenHunter_03 Mar. 19 00.10

Right Click the Project Name and Select “Add Item”, Select ADO.NET Entity Data Model from the “Data” Category and select a name for it

ScreenHunter_05 Mar. 19 00.11

A Wizard will start, Please select “Generate from Database” (Note: Though theoretically, you can start with the Model and the Later on map it to the a database, The Visual studio tools for EF are best designed in this current release to generate the mode from the database.

ScreenHunter_06 Mar. 19 00.11

The Next form will ask you to create the connection to the database Or choose from an existing connection in the project if one exists:

ScreenHunter_07 Mar. 19 00.11

Create the connection. Note that the wizard automatically creates a key in the web.config or app.config with the connection string inside it (nice feature)

Next you will be asked To select the Tables/Views/Procedures that you want to include in the Entity Data Model and to se a namespace for the Model Classes:

ScreenHunter_09 Mar. 19 00.13

In Our case we will simply Select the “products” and “Categories” Tables from the List of tables available in the Northwind database. (This is just for simplicity).

Based on the choice, the Wizard will generate the Entity data model.

ScreenHunter_10 Mar. 19 00.17

To make the Model more easy to understand and more natural, you can set the name for the Entity (Product as opposed to products like the table name in the database) and the Name of the Entity Set (we can make it Products or ProductSet or whatever)

ScreenHunter_11 Mar. 19 00.28

You can also change the Class properties, For example, you can rename ProductName (as in the column name in the DB) to be “Name” to be more natural to write code like productObj.Name rather than productObj.ProductName for example.

 

Now the model is ready we can start retrieving data.

A very simple code to load all products will look like this:

using (NorthwindDBConn context = new NorthwindDBConn())
{
    foreach (Product prod in context.Products)
    {
        Console.WriteLine(prod.Name);
    }
}

Important Note: Though prod Object has a category property, due to Lazy loading by the EF it will e initialized as null and wont be fetched from the database.

In order to read it from the database and make sure it is initialized, you need to add the line “ prod.CategoryReference.Load(); “ so the code will look like :

 

using (NorthwindDBConn context = new NorthwindDBConn())
{
    foreach (Product prod in context.Products)
    {
        prod.CategoryReference.Load();
        Console.WriteLine(prod.Name);
    }
}

 

That is All you need to retrieve elements from EF. To use more sophisticated queries, EF allows you to use LINQ to build queries.

 

In order to save an Product object for example, all you nee to do is:

using (NorthwindDBConn context = new NorthwindDBConn())
    {
        Product x = new Product();
        x.Name = "New Product";

        context.AddToProducts(x);

        context.SaveChanges();
    }

 

That is all you need to get started with the EF.

For more information you can refer to:

Read Full Post »