开发者

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:

  1. Database administrator manually creates a new database and a new user that has access only to that database (database_owner permission)
  2. My application is configured with those database details and the new user's login
  3. 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜