Entity Framework Code First - MySQL - error cannot find table

I am new to EF, EF Code First, and EF with MySQL. When will EF Code First create your tables in an ASP.NET MVC web project?

I created a Person model. Then the controller and standard views are generated. When I click on the Index method of the Person controller, it tries to drop the list of all people. Then I get the error:
An error occurred while executing the command definition. See Internal Exception for more details.
Internal exception:
Table "testmvc.people" does not exist

So, I skipped this connection. But the table was not created. How to create tables? Also, how can I prevent Person to People pluralization in the naming scheme?

+4
source share
2 answers

The easiest way to generate a database schema (people table and others) is to set the database initialization strategy as follows:

Database.SetInitializer<SomeContext>( new DropCreateDatabaseAlways<SomeContext>()); 

This code needs to be executed before trying to load any data, so the Application_Start () method in Global.asax would be a good place to do this. There are several ways to initialize, so you can take a look at them before choosing one, see http://msdn.microsoft.com/en-us/library/system.data.entity%28v=vs.103%29. aspx and see the methods that implement IDatabaseInitializer. There is officially a default strategy, although I never found that I was working for me.

You should also know that although this method is great for prototyping and development, you cannot use it in a production database with live data, since the database is first deleted and then recreated. There are other ways to do this at this point - see Database Migrations for Entity Framework 4 for features.

As for your other question about using names with an unextended table, there are several ways to do this. One way is to annotate the Person class as follows:

 [Table("Person")] class Person { // some field attributes } 

To set this for all tables at once, you can use the free API, for example:

 class SomeContext : DbContext { protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } } 
+8
source

MySql with entity infrastructure requires a bit of tweaking. You need to create three classes (you can check https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql -provider for more information). First create the MySqlHistoryContext class.

 public class MySqlHistoryContext : HistoryContext { public MySqlHistoryContext( DbConnection existingConnection, string defaultSchema) : base(existingConnection, defaultSchema) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired(); modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired(); } 

}

Create the following class MySqlConfiguration

 public class MySqlConfiguration : DbConfiguration { public MySqlConfiguration() { SetHistoryContext( "MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema)); } 

}

Create the following class MySqlInitializer

 public class MySqlInitializer : IDatabaseInitializer<ApplicationDbContext> { public void InitializeDatabase(ApplicationDbContext context) { if (!context.Database.Exists()) { // if database did not exist before - create it context.Database.Create(); } else { // query to check if MigrationHistory table is present in the database var migrationHistoryTableExists = ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<int>( "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'IdentityMySQLDatabase' AND table_name = '__MigrationHistory'"); // if MigrationHistory table is not there (which is the case first time we run) - create it if (migrationHistoryTableExists.FirstOrDefault() == 0) { context.Database.Delete(); context.Database.Create(); } } } } 

Open IdentityModels.cs in the model folder. Add this to the ApplicationDbContext class: IdentityDbContext

 static ApplicationDbContext() { Database.SetInitializer(new MySqlInitializer()); } 
+2
source

Source: https://habr.com/ru/post/1390446/


All Articles