Entity Framework from scratch Part 2 EF4 release

A lot has happened since the last blog where we introduced the entity framework and tried to explain the possible benefits it could have over the other NoSQL.
Microsoft has released a new EF4 that builds us cleaner code, “Code First Development”

They have kept their eyes out on the other NoSQL movements and learned, In this post we are going to introduce 2 new types, the DbContext and the DbSet that EF4 brought us.
At first I was puzzled as in why two new types, and not just remake the ObjectContext and ObjectSet.
But there is a logica behind this, as you can read on the Msdn blog. They are no replacement, they are just simplified. Now you can always choose to use the more advanced types. DBSet has full compatibility with the excisting EF’s and to switch to the ObjectContext and ObjectSet.

We are going to build a standard console app.
First, make sure you're on .Net version 4, or the entity framework 4 will not work.

Dwnload the new EF4 features here and install them.

Fire up your visual studio and choose the console app, and name it ShoeShoe. Now add a using statement on top of the program.cs

using System.Data.Entity;

Now define the two classes we need for our data, namely ShoeShoe a class that describes the shoes in my closet and second the type of shoes i have collected.

public class ShoeCat
{
    public string ShoeCatId { get; set; }
    public string Name { get; set; }

    public virtual ICollection Shoe { get; set; }
}

public class ShoeShoe
{
    public int ShoeShoeId { get; set; }
    public string Name { get; set; }
    public string CategoryId { get; set; }

    public virtual ShoeCat Category { get; set; }

Now we are gonna add context using the new DbContext DbSet type from EF4

public class ShoeShoeCatalog : DbContext
{
    public DbSet Categories { get; set; }
    public DbSet Products { get; set; }
}

To get your console app to recognize the Dbset statement, we need to add 2 references:
The Microsoft.Data.Entity.CTP
microsoft.data.entity
and the system.data.entity.

this is all the code needed to start the process of storing and retrieving data.

Now let’s put some data in the app and build a program class

class Program
{
    static void Main(string[] args)
    {
        using (var context = new ShoeShoeCatalog())
        {
            var Pump = new ShoeCat { ShoeCatId = "Pump", Name = "Sexy" };
            context.Categories.Add(Pump);
            int recordsAffected = context.SaveChanges();

            Console.WriteLine(
                "Succesfully Saved {0} row(s) to the database, press any key to exit.",
                recordsAffected);

            Console.ReadKey();
        }
    }
}

Now we successfully added shoeshoe data.
This is ofcourse just the beginning of our shoeshoe app, but i hope you are bit more familiar with the 2 new types EF4 has provided us.

Enjoy ! Next we are going to build extra features in ourShoeShoe console application.

3 Replies to “Entity Framework from scratch Part 2 EF4 release”

Leave a Reply to online poker Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.