我按照this tutorial进行操作,使用MySQL数据库启动了可正常使用的ASP.NET MVC应用程序。我一路走来,但是发现,启动应用程序时,我覆盖的Seed方法永远不会被调用。
public class MyDbInitializer : DropCreateDatabaseAlways<MyDbContext>
{
protected override void Seed(MyDbContext context)
{
//Roles
context.Roles.Add(new Role { role = "admin" });
context.Roles.Add(new Role { role = "user" });
//Users
context.Users.Add(new User { userName = "test", firstName = "Elliot", lastName = "Alderson", email = "[email protected]", phone = "555-555-5555", city = "New York", state = "New York", zipCode = "10001" });
//User Roles
context.UserRoles.Add(new UserRole { userName = "test", role = "admin" });
//Logins
context.Logins.Add(new Login { userName = "test", password = "test" });
//Credit Cards
context.CreditCards.Add(new CreditCard { userName = "test", creditCardNumber = "1234432156788765", expirationDate = new DateTime(2020, 1, 1), svn = "123", brand = "Evil Corp" });
base.Seed(context);
}
}
在谷歌上搜索,我发现我需要在我的应用程序的web.config文件中添加上下文,该文件嵌套在我执行的entityFramework标签中
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<contexts>
<context type="COS492_SIP.DAL.MyDbContext, COS492_SIP">
<databaseInitializer type="COS492_SIP.DAL.MyDbInitializer, COS492_SIP" />
</context>
</contexts>
</entityFramework>
最后,我更改了global.asax文件,以强制初始化数据库上下文,但该数据库上下文也不起作用
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer<MyDbContext>(new MyDbInitializer());
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
我认为这与MySQL后端数据库有关(我无法切换到其他数据库),因为entityFramework标记引用了mssql。如果是这样的话,我该如何解决此问题,又如何调用seed方法?
编辑
MyDbContext类
public class MyDbContext : DbContext
{
public MyDbContext() : base("MyDbContextConnectionString")
{
Database.SetInitializer<MyDbContext>(new MyDbInitializer());
}
public DbSet<CreditCard> CreditCards { get; set; }
public DbSet<Login> Logins { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserRole> UserRoles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Web Config连接字符串
<connectionStrings>
<add name="MyDbContextConnectionString" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;UserId=root;Password=toor;database=gilbert_sip;CharSet=utf8;Persist Security Info=True"/>
</connectionStrings>
最佳答案
在Application_Start
中尝试以下操作:
System.Data.Entity.Database.SetInitializer<MyDbContext>(new MyDbInitializer());
var db = new MyDbContext();
db.Database.Initialize(true);