简体   繁体   中英

MySql EF Code First migrations VB.NET

I have been playing around with Entity Framework and MySql using the Code First approach,

When I run my code against a newly created MySql Server with no databases etc (apart from the defaults) It creates the database and tables according to the classes I have created. My issue is and I don't know if I'm doing this right is, if I make a change to my model class for example add a new property I would expect a new column to be added to that particular table. However this isn't the case instead I'm getting an error

Unknown column: 'Extent1.Email' in 'field list'

I know this is because I have made a change to the class, but I am under the impression that EF would be able to make that change automatically to the database.

I have Installed

EF 6.1.1

MySql.Data 6.9.3

MySql.Data.Entity 6.9.3

All your db initialization takes place in

 Interface IDatabaseInitializer(Of In TContext As System.Data.Entity.DbContext)

You can set it in your DbContext s constructor using Entity.Database.SetInitializer() or configure it in your Web.config in <entityFramework><contexts><context><databaseInititalizer> .

To get your new columns in your existing database, you need to either do the necessary migrations (see http://msdn.microsoft.com/en-us/data/JJ591621.aspx ) or you drop and recreate the database on every model change (which is what the DropCreateDatabaseIfModelChanges implementation of IDatabaseInitializer does, see http://www.adfa.se/Archive/2013/02/01/database-initializers-in-ef-code-first ).

Without further information I recommend you to do the latter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM