技术开发 频道

实战.NET MVC3及Entitiy Framwork框架

  步骤4:建立Customer实体类,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace CodeSimplified.Models
{
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
}
}

  接下来,我们还需要建立一个类MyDataContext,它继承了EF框架中的工具类DbContext,在这个MyDataContext类中,我们重写了其中的OnModelCreating方法,代码如下:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;
namespace CodeSimplified.Models
{
public class MyDataContext:DbContext
{
public DbSet<Customer> Customer { get; set; }
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove
<PluralizingTableNameConvention>();
}
}
}

  因为EF框架中,默认是存在规则,将实体类中的POJO文件对应生成数据库中的表的,在命名上,假如比如实体类名为Product,它会智能映射成Products的表,加了个"s",然而,Category的实体类却映射成了Categories,因此我们可以在OnModelCreating中去掉EF内置的命名规则,即添加语句:

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

  步骤5:添加Controller

  现在,我们要添加控制层了,在项目文件中添加controller文件夹,增加一个Controller文件,命名为CustomerController,如下图:

实战.NET MVC3及Entitiy Framwork框架

0
相关文章