Entity Framework create table without "database create" permission?
I am developing with Entity Framework code first and SQL Server 2008.
I want to support this common installation scenario:
- Database administrator manually creates a new database and a new user that has access only to that database (
database_owner
permission) - My application is configured with those database details and the new user's login
- My application uses Entity Framework to automatically initialize the database (create the tables, views, etc)
However, when I try to get Entity Framework to initialize the database:
context.Database.Create();
I will get an exception, because t开发者_Go百科his method tries to create the database, not just create the tables, views, etc inside the database.
How can I get Entity Framework to initialize a database with a user that does not have "create database" permissions, using a database that has already been created for it?
There is an implementation of IDatabaseIntializer that only creates the tables:
http://nuget.org/List/Packages/EFCodeFirst.CreateTablesOnly
There are also good solutions here:
Entity Framework CTP 4 - Code First Custom Database Initializer
Entity Framework 4 Code First - Prevent DB Drop/Create
Irrelevant of the technology being used, application should separate deployment privileges from normal use privileges. Is perfectly acceptable to require higher privileges for deployment and you should follow the same model. In other words your deployment (read: 'Setup' or 'MSI' or 'Installation') should require higher privileges (eg. dbcreator membership) and, under this elevated context of deployment, create the 'code first' model. So simply move the 'context.Database.Create' into your application Setup and have an administrator install the application.
精彩评论