Database Reference
In-Depth Information
As you know, we selected the template of empty read/write actions while adding CategoryController . It will
create all of the dummy action methods in the controller code. The DbContext code for this recipe is shown in
Listing 4-1.
Listing 4-1. Inherited DbContext Class to Perform Operations in the Database Using Entity Framework
namespace MyStore.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MyStoreEntities : DbContext
{
public MyStoreEntities()
: base("name=MyStoreEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<App> Apps { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Developer> Developers { get; set; }
}
}
We have to change the code, as shown in Listing 4-2, in these action methods to perform insert, delete, edit, and
view on models using DbContext class.
Listing 4-2. Controller Code for Category Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyStore.Models;
namespace MyStore.Controllers
{
public class CategoryController : Controller
{
// GET: /Category/
 
Search WWH ::




Custom Search