Am I doing it right or there are bugs left on Entity Framework 4.1 and Code First?

Spring is here and love is in the air, I have fallen in love with EF Code First, not database first like other people that likes to design databases. I like to write code and somebody else can create the database for me that looks like my classes and properties, this time my DBA’s name is Entity Framework 4.1 Code First, that will create the database to store my data represented with my classes.

Now, because code is very flexible, I can do it in many ways it seems, I’m afraid to be doing it wrong.

First I create my classes, instead of products and blogs like other people, I’ll do something more exciting, twitter users Winking smile Under the directory model I add the TwitterUser.cs class

   1:  namespace Sample1.Models
   2:  {
   3:      public class TwitterUser
   4:      {
   5:          public string ID { get; set; }
   6:          public string Username { get; set; }
   7:          public DateTime LastPull { get; set; }
   8:   
   9:          public virtual ICollection<Follower> Followers { get; set; }
  10:   
  11:          public virtual ICollection<Unfollowers> Unfollowers { get; set; }
  12:      }
  13:   
  14:      public class Follower
  15:      {
  16:          public virtual TwitterUser TwitterUser { get; set; }
  17:          public string ID { get; set; }
  18:          public string Username { get; set; }        
  19:      }
  20:   
  21:      public class Unfollowers
  22:      {
  23:          public virtual TwitterUser TwitterUser { get; set; }
  24:          public string ID { get; set; }
  25:          public string Username { get; set; }
  26:          public DateTime FoundTime { get; set; }
  27:      }
  28:   
  29:      public class TwitterUserContext : DbContext
  30:      {
  31:          public DbSet<TwitterUser> Tweeps { get; set; }
  32:          public DbSet<Follower> Followers { get; set; }
  33:          public DbSet<Unfollowers> Unfollowers { get; set; }
  34:      }
  35:  }

Yes the classes do not need to be inherit from any other class, that is cool, yet I would like to set an attribute to tell it to create a table from that class, instead I use another class that inherits from DbContext to add them into a DbSet. This is confusing to me, I would just prefer to use attributes. Looks like the patters sometime chance at Microsoft.

Once the 3 classes be added at the DbSet as properties, now let’s start using it.

I create in MVC 3 Refresh 1 a controller to request for user details.

   1:  public ActionResult Details(string sUserName)
   2:          {
   3:              Database.DefaultConnectionFactory =
   4:                   new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");
   5:   
   6:              Sample1.Models.TwitterUserContext dbContext = new Models.TwitterUserContext();

At line 3 and 4 you can see I force Code First to use the Compact Database instead of the default Sql Express. It’s your preference, as well you can make a change to your web.config so you do not have to use code. However in today’s blog post, everything is about Code First.

Now first issues I see with EF Code First, in line 1 below, I have to put a try and catch, as first time that creates the database the dbContext.Tweeps will be not just null will actually throw and exception when trying to run it. Looking if the dbContext is null as well, yet the database is already being created the first time that I try to access it. Should I check any property to see if the database is being created? I see that there is an initialize property.

   1:              try
   2:              {
   3:                if ( dbContext.Tweeps != null )
   4:                  if (dbContext.Tweeps.Count() > 0)
   5:                  {
   6:                      List<Models.TwitterUser> userlist = dbContext.Tweeps.ToList();
   7:                      foreach (Models.TwitterUser us in userlist)
   8:                      {
   9:                          if (us.Username == sUserName)
  10:                          {
  11:                              myUser = us;
  12:                              break;
  13:                          }
  14:                      }
  15:                  }
  16:                }
  17:              }
  18:              catch { }

 

Add

Creating a new Twitter User is simple and so elegant in the sample, is the part that got me hooked into CF. Its as simple of adding into a collection, the essence of .NET.

   1:              if (myUser == null)
   2:              {
   3:                  myUser = new Models.TwitterUser()
   4:                  {
   5:                      Username = sUserName,
   6:                      ID = Guid.NewGuid().ToString()
   7:                  };
   8:   
   9:                  dbContext.Tweeps.Add(myUser);
  10:                  dbContext.SaveChanges();
  11:              }

 

Modify

Now, modify is confusing to me, I would think that if you change any property, the item gets marked as dirty and then when calling SaveChanges, will make the update.  Line number 2 below should be redundant, I know that because I created the class and the properties, they cannot add a dirty flag at the set, yet I would like something that I do not need to flag the item as modified. However this is how you have to do it.

   1:  myUser.Username = "alpascual";
   2:  dbContext.Entry(myUser).State = System.Data.EntityState.Modified;
   3:  dbContext.SaveChanges();

 

Delete

Now, I hope you are not cut and pasting my code, as the one below has to be wrong, must be a better way to delete one by one the items on my table.

   1:  while (myUser.Followers.Count > 0 )
   2:  {
   3:           Models.Follower foll = myUser.Followers.ElementAt(0);
   4:           dbContext.Entry(foll).State = System.Data.EntityState.Deleted;                        
   5:  }
   6:                                          
   7:   dbContext.SaveChanges();

Can I use LINQ to delete? I would like to delete only a few based on a query.

In any case, Code First is the coolest thing, I cannot wait for learning it and improving it until I do it right. A few places to learn. I would start from this 2 below. Scott Guthrie and Scott Hanselman.

Scott Gu’s blog post about EF Code First tutorial

Scott Hanselman presentation as always entertaining about EF CF

Cheers

Al

6 Comments

  • You don't need to use the flag for marking the dirty fields on an update. I think the problem is on your model, you need to make all the properties virtual so EF can create a proxy on top of your classes and mark the properties as dirty when something changes. About the database creation, if you use a DI approach, the context is tipically instanciated by the DI container and later injected into your MVC controller (in your example), so you already have the DB created and a valid context at that point.

    Regards
    Pablo.

  • Whether you need。
    Gets into our website for a minute that uses you
    http://www.fashionjeanshop.com
    We are international trade. We offer grade a quality product only. Our products are authentic quality with original box .No matter what reason it is. buy from us will have much more confidence ! So, please don’t hesitate, just contact us for details! We will be your reliable business partner!
    Hope you guys will enjoy by online shopping without much disturbances.

  • ?????????????????????????????????????????????

  • ElYfmL This is one awesome blog article. Really Cool.

  • ZoKI4X I really liked your blog.Thanks Again. Want more.

  • O0jhce Say, you got a nice blog.Thanks Again. Keep writing.

Comments have been disabled for this content.