开发者

How to get "code first" to update tables in mysql?

When having c# .net4 mvc3 and entity-framework 4.1 and MySql 5.0.67 using Connector/NET 6.4.3 as the stack. What do I need to configure in order to have the app update the tables in MySql when there is a change in a domain class?

Additionally, is there a way to generate sql/DDL from the domain classes?

I want to use the "code first" method but I can't seem to get it to update/create tab开发者_C百科les when the database already exist.

When changing the connection provider and string to be a SQLServer CE it creates both the database and the tables.


I'm trying to do something similar except I am trying to use SQLite. I've just found this page: http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx that has a section titled "Get the Code First Generated SQL DDL", that seems like it should help with generating the SQL to create the database.

Have you looked at the Database.SetInitializer method (http://msdn.microsoft.com/en-us/library/gg679461%28v=vs.103%29.aspx) one type it uses is DropCreateDatabaseIfModelChanges (http://msdn.microsoft.com/en-us/library/gg679604%28v=vs.103%29.aspx). This also worked for me with SQLServer CE but I think I found that the current version of the System.Data.SQLite does not support this yet so I will have to create the database in another way.

Here are a couple of other questions that might be of some help, though I cannot be sure that they can apply to code first.

Entity Framework 4.1 - Tracking and manually deploying DDL changes using T-SQL Entity Framework 4 - Update database schema from model. Without wiping the table data

Good luck with it. I'm posting my progress with coding in general (will include EF 4.1 as I work with it) on my website kihonkai.com if your interested in taking a look.


I answered a similar question here: Entity Framework 4.1 Code First not creating tables

Basically, it involves:

  • Creating a YourDbContextInitializer which inherits from DropCreateDatabaseIfModelChanges<YourDbContext>
  • Override the Seed() method in YourDbContextInitializer to fill your database with initial data.
  • Overriding the OnModelCreating(ModelBuilder modelBuilder) method of YourDbContext and setting the database initializer using Database.SetInitializer(new YourDbContextInitializer());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜