私はthis tutorialに続いて、MySQLデータベースを使用して動作するASP.NET MVCアプリケーションを取得します。私はそれを完全に理解していますが、私のアプリケーションを起動するとき、私が上書きしたSeedメソッドは決して呼び出されないことを発見しました。周りグーグルコード最初のASP.NET MVC App、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);
}
}
、私は
<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ファイルを変更しentityFrameworkタグにネストされて私のアプリのweb.configファイル内のコンテキストを追加するために必要な発見また
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);
}
}
が、私はこれはENTので、(私は別のものに切り替えることはできません)MySQLのであるバックエンドデータベースとは何かを持っていると思います動作しませんでしたデータベースコンテキストを初期化する強制的にityFrameworkタグは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>
もし私が間違っていないのであれば、 'Seed()'が呼び出されるようにパッケージマネージャーコンソールで 'update-database'を実行する必要があります。 –
このエラーが発生しましたアセンブリ 'COS492_SIP'に移行設定タイプが見つかりませんでした。 (Visual Studioでは、パッケージマネージャコンソールのEnable-Migrationsコマンドを使用して移行設定を追加できます)。 –
web.configにデータベースを指す ''エントリがありますか? MySQLの設定方法はわかりませんが、 ''セクションにあります。少なくともSQL Serverでは、 'defaultConnectionFactory'に加えてそれが必要です。 –