问题
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.
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:
"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".
My 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>
Any help much appreciated.
回答1:
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());
...
With this you would remove the <context configuration. (but keep the connectionStrings/add config)
来源:https://stackoverflow.com/questions/27713276/code-first-database-not-being-created-from-model