MVC 5+EF 6 OnModelCreatingメソッドDbSet名対応修正

3439 ワード

MVC 5+EF 6開発練習
データベースは先に設計されているため,名前の面では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");
        }
    }
}