代码第一个数据库不是从模型创建的

时间:2014-12-30 22:07:36

标签: asp.net entity-framework

我正在使用asp.net和实体框架创建一个基本的MVC应用程序。我一直关注this tutorial,改变数据模型以满足我的需求。

我已经创建了一个数据库模型,并生成了MVC控制器。当我运行应用程序并导航到需要数据库的页面时,当到达控制器中的行return View(db.Servers.ToList());时,我得到以下错误:

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

有两个内部例外:“使用相对路径时,请确保当前目录正确”和“验证文件是否存在于当前位置”。

我的代码

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>

任何帮助都非常感激。

1 个答案:

答案 0 :(得分:0)

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

更强类型的方法是:

public class serverContext: DbContext 
{

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

使用此功能,您将删除<context配置。 (但保留connectionStrings / add config)

相关问题