首页 > 代码库 > 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名称对应
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。