by August 2, 2011 0 comments



Vinod Unny, Enterprise InfoTech

Microsoft’s ASP.NET MVC3 already added a huge bunch of features to the web development stack in Visual Studio 2010. But barely 3 months after the release of MVC3, had Microsoft released an update — not to the stack itself but for the tools integrated within Visual Studio. This update actually adds a bunch of features that allow you to quickly start working with data and other benefits within VS2010.

Snapshot

Applies: .NET developers
USP:earn about the Entity Framework Code and other such features that let you create a code structure for your data.
Related articles: Building Web Apps With ASP.NET MVC3 – http://ld2.in/3ag
The ASP.NET Razor Syntax – http://ld2.in/3ks
Search engine keywords:MVC3

Among some of the changes that we’ll talk about is the new feature called Entity Framework Code-First data modeling. In this you can use EF 4.1 to create a code structure of what your data schema should be and EF will automatically create the database file for you. Create a simply MVC3 Razor based Web application and install the MVC3 update, EF 4.1 and if possible SQL Compact edition. Make sure you have NuGet Package manager installed and then simply install the EntityFramework.SQLCompact package. This will also install all the required dependencies.

[image_library_tag 306/67306, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]

Now in the “Models” folder, create a new class file. Create a couple of classes in it with some fields. If you need to have a relationship between them, use the virtual keyword. For example, to create a Music store database with two tables — Genre and Album they would look like this:

[image_library_tag 310/67310, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]


public class Album
{
public int AlbumId { get; set; }
public string AlbumName { get; set; }
public string Year { get; set; }
public int GenreId { get; set; }
public virtual Genre Genre { get; set; }
}
public class Genre
{
public int GenreId { get; set; }
public string GenreName { get; set; }
public virtual ICollection Albums { get; set; }
}

Note that both contain a reference to each other.

[image_library_tag 312/67312, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]

Save the file and create a new controller — say for the Genre. In the dialog that comes up, select the name of the class you wish to use for the data and since this is the first class you are coding for, create a new datacontext class. For all other controllers, you can reuse this new datacontext class as well.

[image_library_tag 313/67313, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]

The cool thing is that if you select the “Controller with read/write actions and views” in this dialog, you almost have to do no more work to get the entire code running. Simply compile and execute the Web application and browse over to the http:///Genre to get to the page to work with the Genre data. You can add, edit, delete and view the data easily from this page.

[image_library_tag 315/67315, border=”0″ align=”middle” hspace=”4″ vspace=”4″ ,default]


But where does the data actually get stored? After all, we only created a class — not the physical database itself! This is done using the EF Code-First tool that creates an SQL Compact database file in the App_Data folder. The cool part about SQL Compact is that you can simply take the binaries of the system and dump it in the Web application to get the database “server” running for that application. You can of course migrate easily to SQL Server once you need to for scalability later on.

The great part about the tools refresh is that it takes care of a lot of mundane chores for you. For instance, perform the same steps for the Album class — that is create the controller for it and let the actions and views by generated automatically. Browse over to the Album page and create a new one. You will see that it has automatically made the association with the Genre table and the genres are shown as a dropdown in this page.

MVC3 has a number of other capabilities as well that lets developers create powerful web application easily and faster. This month you’ve looked at working with data and how easy it is to create an application that even creates the database for you.

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.