问题
I am doing a project, where I am going to use Entity Framework 4.1. I am planning on doing the "Code-First" approach, as I like clean entities and I want to extend them in some fashion.
I have an existing empty database, that I would like to use. I have setup the connection string as below, and I have set EF to always drop and create database (to start with). And this is my problem.
I have access to this empty database, but I don't have permissions to drop it and/or create it. The database is already there ready to use. How can I get EF to use the database as it is, instead of dropping and creating it?
By the way, if I doesn't set it to drop and create, it complains about that it cannot find the tables, which is understandable.
回答1:
In order to use EF Code First with an existing empty DB you will need to use a custom database initializer, that does not recreate the DB but only the tables, luckily somebody did this already:
http://blogs.microsoft.co.il/blogs/gilf/archive/2011/05/30/creating-a-code-first-database-initializer-strategy.aspx
You should also have a look at the new Database Migration functionality of EF 4.3 this becomes important as your schema evolves.
来源:https://stackoverflow.com/questions/9796612/code-first-with-existing-empty-database