Wednesday, May 01, 2013

Seeding Junction Tables with EF Code First

My stint with Code First
My first stint with Code First was when I attended a code camp at Lebanon Valley. Mike Lukatchik (@mlukatchik) did a very energetic and informative session of Code First. He emphasized that code first is very useful in an agile environment. I kind of saw where it could be used, and I was very impressed with how he presented. But I should admit that Code First could be practical enough for a enterprise environment. I could almost hear the DBAs getting angry! :)

I was so impressed with Mike's presentation, that I invited him over to present the same session at the Central Penn .NET User Group's monthly meeting. Needless to say, it was one of the best presentations ever. Thanks, Mike!

What is Code First?
There are umpteen number of articles around Code First. But to give a simple answer, code first with Entity Framework is a technique where we create Entities first, instead of creating database objects. The database objects gets created by the Code First framework. If you are knew to Code First, I suggest you to take a plunge into this article -

Our Sample Entities
Now, let us dive into the core of the article. Suppose we have two entities - User and Role. And lets say, a User has more than one Role. And a Role could be shared among many Users. You are right - a Many-to-Many relationship. And, as you know, the Entities in EF are nothing but POCOs. Plain Old CLR Objects.

public class User
   public int UserId {get; set;}
   public  List Roles {get; set;}
public class Role
   public int RoleId {get; set;}
   public  List Users {get; set;}
Once you run the migration, the corresponding tables get created. Like the ones below:

--> Screen shot of tables in the database

Junction Table

Where is the relationship though, you may ask.

Well, do you see the collection of Roles entities in User entity? And do you see the collection of User entities in Role entity? The Entity Framework is smart enough to take these into account and create a "THIRD" table - a Junction table.

Note that the junction table DOES NOT have a corresponding Entity in our context. That is because we do not need it. We could use either User entity or Role entity to get the relevant collection data.

Seeding Data
Many a times, to test the application, we need seed data - test data, that is. The Configuration.cs file that EF creates, is used for placing the code to seed the data.

A Footnote:
In my new project, we use Code First. On my very first day, when my Technical Architect told me that we are using Code First,  all the slides and code from Mike's presentation poured in. Even if I didn't know the intricacies of Code First technique, I did not have a starting trouble - it just took 20 minutes of a sample code and little bit of reading to get into the groove. That was all because I had attended Mike's presentation.  Many of my programmer friends and colleagues do not see the benefit of attending a user group meeting or a code camp. Well, here is one! :) The biggest (and the lamest:)) argument I every hear is that "Oh, we wouldn't be using this technology… "… Well, you never know :)

No comments:

Post a Comment