New Releases for ASP.NET MVS 3 and Entity Framework CTP

Since I posted my last blog on building ASP.NET MVC 3 application using Razor view engine and Entity Framework code first CTP 4, both of these technologies underwent updates.  Both are covered on Scott Guthrie’s blog.  You can read MVC 3 RC 2 announcement. and Entity Framework CTP 5 announcement.

In this post I will cover the change I had to make to compile my existing project.  Frist of all, I downloaded and installed both updates.  Here is the download link to EF CTP 5.  This link points to MVC 3 download.

Once I downloaded and installed both, I had a slew of compiler errors.  View property of the controller and VIew property of the VIew are now called ViewBag.  It is dynamic type property and you use the following syntax to access it:

this.ViewBag.Message = "Welcome to ASP.NET MVC!";

Similar syntax is used in views:

@{
    ViewBag.Title = "About Us";
}

 

This was the major change, but it was fairly mechanical. 

Entity framework changes were simple as well.  Database classes is now called DbDatabase to match other naming conventions.  Class that controls if database is recreated when model changes is now called DropCreateDatabaseIfModelChanges. Once these few changes were done I was able to run the application again.  You can download updated application here.

You can see full list of change in MVC RC 2 here.

List of changes to Entity Framework going to CTP 5 is available in ReadMe file on the download page I pointed to above.  I am going to highlight the key points below.  These are applicable to code first as a whole, not necessary to CTP 5 itself.

  • T4 templates for code first classes are included.  As a result, you can reverse engineer a model first or database first models into a set of DbSet/DbContext classes.  If you have existing database, and you want to switch to use code first approach, the process is very simple.
  • You have access to entity states (Added, Unchanged, Modified, Deleted) and property values for each property values.  Here is how you can access entity state and property values
context.Entry<BlogEntry>(entry).State = System.Data.EntityState.Unchanged;
var text = context.Entry<BlogEntry>(entry).OriginalValues["PostText"];
var newText = context.Entry<BlogEntry>(entry).CurrentValues["PostText"];

 

  • There is ability to explicitly load navigation property’s data.
context.Entry<BlogEntry>(entry).Collection<BlogCategory>().Load()

 

  • You have a property that returns ObservableCollection for a set of entries.  This is useful in WPF applications.

ObservableCollection<BlogEntry> collection = context.Entries.Local;

  • You can get data without tracking which reduces the overhead of getting the data.  You have to import System.Data.Entity namespace
var data = blogs.Skip((currentPage - 1) * pageSize).Take(pageSize).AsNoTracking().ToArray();

 

  • There is a lot of change information available from context.ChangeTracker property.
  • You can execute raw SQL Commands.  Those are accessible through context.Database.SqlCommand (akin to ExecuteNonQuery) and context.Database.SqlQuery and context.Database.SqlQuery<T> both akin to ExecuteQuery and return a collection of objects based on type you specify.  You are not responsible for materializing the results of this queries, which is pretty cool.
  • If you do not want to use EdmMetadata table which is used to detect changes to model that database cannot accommodate, you can turn this off by removing a convention from model builder.
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();

 

I am going to cover more changes in the next post.

 

Leave a Reply

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