Question

I have a pre-existing database and Entity Framework 6 database-first model generated from it. I need to create the same database on a different host and/or database engine. I hoped I could use the same model as code-first, but OnModelCreating method of the generated DbContext seems to be protected from being used for code-first approach, it throws UnintentionalCodeFirstException. Generated models don't have any attributes applied too.

Is there any way to reuse my database-first model to create a database with the same schema? Or maybe there's an obvious and much simpler way to do it that I'm missing?

Était-ce utile?

La solution

If you just want to create the database you shouldn't need to touch the OnModelCreating method. You should be able to do this from the designer (right click on the designer surface and select "Generate Database From Model" and run the script) or if you want to do this from code do ctx.Database.Create() or use the CreateDatabaseIfNotExists initializer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top