码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2014-12-19 12:02:34      阅读:619      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   使用   sp   for   

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名称对应

标签:style   blog   http   io   color   os   使用   sp   for   

原文地址:http://www.cnblogs.com/bmbh/p/4173510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!