ASP.NET MVC: EF 没有创建数据库表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET MVC: EF 没有创建数据库表相关的知识,希望对你有一定的参考价值。

设定连接字符串时,Name属性的值,应该等于创建的EF数据库类类名

 

比如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using MVC_example.Models;

namespace MVC_example.DataAccessLayer
{
    public class Cnn : DbContext
    {
        private DbSet<User> _users;

        public DbSet<User> Users
        {
            get
            {
                return _users;
            }

            set
            {
                _users = value;
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().ToTable("tUser");

            base.OnModelCreating(modelBuilder);
        }
    }
}

 其中定义了类名为Cnn,则Web.config中的ConnectionString

应为:

<connectionStrings>
    <add connectionString="Data Source=.;Initial Catalog=SalesERPDB;Integrated Security=True;" name="Cnn" providerName="System.Data.SqlClient" />
  </connectionStrings>

 

以上是关于ASP.NET MVC: EF 没有创建数据库表的主要内容,如果未能解决你的问题,请参考以下文章