code第一个数据库没有从模型创建 [英] Code first database not being created from model

查看:105
本文介绍了code第一个数据库没有从模型创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用asp.net和实体框架的一个基本的MVC应用程序。我一直在下面<一个href=\"http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application\"相对=nofollow>本教程,改变了数据模型,以满足我的需求。

I'm creating a basic MVC application using asp.net and the entity framework. I've been following this tutorial, altering the data model to fit my needs.

我已经创建了一个数据库模型,以及所产生的MVC控制器。当我运行应用程序并导航到需要我得到以下错误数据库页面时行返回查看(db.Servers.ToList());在控制器内到达:

I've created a database model, and generated the MVC controller. When I run the application and navigate to a page that requires the database I get the following error when the line return View(db.Servers.ToList()); within the controller is reached:

'System.InvalidOperationException'类型的异常出现在EntityFramework.dll其他信息:无法设置类型的数据库初始化Purely_Servers.DAL.serverInitializer,Purely_Servers'的类型的DbContextPurely_Servers.DAL.serverContext,Purely_Servers在应用程序配置中指定。

"An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll. Additional information: Failed to set database initializer of type 'Purely_Servers.DAL.serverInitializer, Purely_Servers' for DbContext type 'Purely_Servers.DAL.serverContext, Purely_Servers' specified in the application configuration."

有两个内部异常:当使用相对路径,确保当前目录是正确的和验证文件存在于当前位置

There are two inner exceptions : "When using relative paths, make sure the current directory is correct" and "Verify the file exists at the current location".

我的code

serverContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Purely_Servers.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace Purely_Servers.DAL
{
    public class serverContext : DbContext
    {

        public serverContext() : base("serverContext")
        {
        }

        // create a DbSet property for each entity set
        public DbSet<server> Servers {get; set;}
        public DbSet<user> Users {get; set;}
        public DbSet<faculty> Faculties {get; set;}
        public DbSet<admin> Admins {get; set;}

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

serverInitializer.cs

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

namespace Purely_Servers.DAL
{
    public class serverInitializer : System.Data.Entity.DropCreateDatabaseIfModelChanges<serverContext>
    {
        protected override void Seed(serverContext context)
        {
            // create the servers
            var servers = new List<server>
            {
                new server{.......}
            };
            // add them to the dbcontext
            servers.ForEach(s => context.Servers.Add(s));
            context.SaveChanges();


            // create the admins
            var admins = new List<admin>
            {
                new admin{.....}
            };
            // add them to the dbcontext
            admins.ForEach(a => context.Admins.Add(a));
            context.SaveChanges();

            // create the users
            var users = new List<user>
            {
                new user{......}
            };
            // add them to the dbcontext
            users.ForEach(u => context.Users.Add(u));
            context.SaveChanges();

            // create the school
            var faculties = new List<faculty>
            {
                new faculty{.....},

            };
            // add them to the context
            faculties.ForEach(f => context.Faculties.Add(f));
            context.SaveChanges();
        }
    }
}

的Web.config

<connectionStrings>
  <add name="serverContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=235Servers;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
  <contexts>
    <context type="Purely_Servers.DAL.serverContext, Purely_Servers">
      <databaseInitializer type="Purely_Servers.DAL.serverInitializer, Purely_Servers" />
    </context>
  </contexts>
</entityFramework>

任何帮助非常AP preciated。

Any help much appreciated.

推荐答案

从配置设置数据库初始化很难得到正确的,因为你的根命名空间,程序集名称和DLL名称都可以是不同的。

Setting the database initializer from config is difficult to get right because your root namespace, assembly name, and DLL name can all be different.

一个更强类型的方法是:

A more strongly typed approach would be:

public class serverContext: DbContext 
{

    public serverContext(): base("serverContext") 
    {
        Database.SetInitializer<serverContext>(new serverInitializer());
        ...

有了这个,你会删除&LT;背景配置。 (但保留是connectionStrings /添加配置)

With this you would remove the <context configuration. (but keep the connectionStrings/add config)

这篇关于code第一个数据库没有从模型创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆