Friday, September 6, 2013

Creating a new database backed ASP.NET MVC Application using Entity Framework

This post is more of a reminder to myself on how to setup and pass data through a simple MVC app.


Creating a new data driven MVC Application (MyAppMVC) using Entity Framework

Add projects
MyAppMVC.Data.Model for the data model and MyAppMVC.Data for the DataContext (with dbset<> ) and migrations folder (include configuration.cs and then add a Seeder class)

Add an Adapters Folder with subfolders Data, Interface and Mock to the main (startup) project.

Add References between the projects:
MyAppMVC references MyAppMVC.Data and MyAppMVC.Data.Model
MyAppMVC.Data references MyAppMVC.Data.Model

Unit test References if needed

Add the Entity Framework Package (NuGet)

Add Data Model classes to .Data.Model

Add ------Context.cs class to .Data (it is a :DbContext)
in this class add DbSets (public DbSet<----> -------s { get; set; }

In Package Manager enable-migrations under .Data --- a Migrations folder with
Configuration.cs file is added

Seed data via Configuration.cs
Add a Seeder.Seed(context) to Configuration and create a Seeder file with a Seed method that takes the dbcontext as a parameter. using System.Data.Entity.Migrations (for AddOrUpdate extension method)

public static void Seed(-----Context context)
        {
            context.------.AddOrUpdate(c => c.----, new ---- { Name="James" });
        }

add-migration initial and then update-database. Open SQL Server Management Studio or VS Database Explorer and celebrate.  VS use (LocalDb)\v11.0

To move data up the stack we use an adapter class to fetch data from the database and return a view model. 

Create a new cotroller (Home) and right click then add a view(Index).

Next add a view model that contains the data the view needs. (Right click Model (top level) and add a .cs class called -------ViewModel ).  Add the data needed from using .Data.Model project .

Right click Interface in Adapter and add an I----Adapter interface that has a method Get------ViewModel that returns the desired view model.  Implement the interface in the Data (and Mock) folders.
 public class ----Adapter : -----.Adapter.Interface.I----Adapter
    {
        public Models.------ViewModel Get------ViewModel()
        {
// create the viewmodel and dbcontext to work with
            ----Context db = new ----Context();
            -----ViewModel model = new -----ViewModel();
            model.----- = db.-----.ToList();    //.FirstOrDefault() for 1 item
// filter with .Where and use .take(#) .orderby() ......
            return model;      
        }
    }
// the adapter queries the database and returns our view model

Now we utilize the adapter in the Controller Action and return the view model to the view.
 -----Adapter _adapter;
        public HomeController()
        {
            _adapter = new -----Adapter();
        }

        public ActionResult Index()
        {
            return View(_adapter.Get------ViewModel());
        }

In the view we specify a view model
@model -----MVC.Models.-----ViewModel        
to loop through a List item with razor
@foreach(var element in Model.-----)
{
@element.-----
}

We have now built a database using code first and the Entity Framework. We then queried that database and passed the results up to a view model.

No comments:

Post a Comment