gpt4 book ai didi

c# - Code First ASP.NET MVC 应用程序,未调用种子方法

转载 作者:行者123 更新时间:2023-11-29 11:14:31 27 4
gpt4 key购买 nike

我关注了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 = "ealderson@allsafe.com", 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。如果是这种情况,我该如何解决这个问题,无论哪种方式,我如何调用种子方法?

编辑

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);

关于c# - Code First ASP.NET MVC 应用程序,未调用种子方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966699/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com