首页 > 代码库 > MVC5 + EF6 OnModelCreating方法修改DbSet名称对应

MVC5 + EF6 OnModelCreating方法修改DbSet名称对应

MVC5+EF6 开发练习

由于数据库是先设计好的,在名称方面不能使用EF的“约定”。导致出现了很多问题。

需重写OnModelCreating方法

代码如下:

using System;using System.Collections.Generic;using System.Data.Entity;using System.Data.Entity.ModelConfiguration.Conventions;using System.Linq;using System.Web;namespace WebApplication2.Models{    public class thContext : DbContext    {        // You can add custom code to this file. Changes will not be overwritten.        //         // If you want Entity Framework to drop and regenerate your database        // automatically whenever you change your model schema, please use data migrations.        // For more information refer to the documentation:        // http://msdn.microsoft.com/en-us/data/jj591621.aspx            public thContext() : base("name=thContext")        {        }        public System.Data.Entity.DbSet<WebApplication2.Models.thEnterprise> th_Enterprise { get; set; }        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {            base.OnModelCreating(modelBuilder);            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();            //Map schemas             modelBuilder.Entity<WebApplication2.Models.thEnterprise>().ToTable("th_Enterprise");        }    }}

 

MVC5 + EF6 OnModelCreating方法修改DbSet名称对应