2

I'm trying to create a database model with Code First, without creating the database itself. That is, my DBA has set up an empty database, for which I have permission to create tables, but not a whole database. However, whenever I try to use Code First to set up the model, I get the following error:

CREATE DATABASE permission denied in database 'master'.

Is this just the way Code First works, or is there some way to modify the existing database?

P.S. -- here's the connection string --

<add name="HoursDb"         
     connectionString="Data Source=barksql.cedarville.edu;
                       Initial Catalog=Hours;
                       persist security info=True;
                       User ID=hours;
                       password=************;
                       multipleactiveresultsets=True;
                       App=EntityFramework"
     providerName="System.Data.SqlClient" />

Update

Per @devdigital's request --

Here is the context class:

using System.Data.Entity;
using ....Models;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace LibraryAdmin.DAL
{
    public class HoursDb : DbContext
    {
        public DbSet<DaySpec> DaySpecs { get; set; }
        public DbSet<WeekSpec> WeekSpecs { get; set; }
        public DbSet<ExceptionHoursSet> ExceptionHoursSets { get; set; }
        public DbSet<Schedule> Schedules { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

As far as mapping the database to the connection, I am assuming it is done by a convention since the HoursDb classname matches the connection string name.

Bondolin
  • 2,793
  • 7
  • 34
  • 62
  • Can you show your configuration (mapping) code? and your context? – devdigital Aug 01 '12 at 15:04
  • 1
    Have a look at Slauma's answer here - http://stackoverflow.com/questions/5584546/ef-4-1-cf-create-database-permission-denied-in-database-master – devdigital Aug 01 '12 at 15:32
  • I think you're right about the mapping between the db and the connectionString. Are you certain your DBA created the database and named it properly? – Forty-Two Aug 01 '12 at 16:53
  • @devdigital -- Hm. I tried Slauma's idea of using Database.SetInitializer(null); and it seemed to give some measure of progress, but failed login because the db was dropped. I am not sure, but that statement may have dropped it. At any rate, my DBA granted me create access to the db and things seem to work; I may ask him to remove that permission and try Slauma's idea again, on an un-dropped db. Thanks for the word back. – Bondolin Aug 01 '12 at 17:11
  • @Forty-Two -- Thanks; the db was dropped. Not sure when or how; investigation pending. – Bondolin Aug 01 '12 at 17:13
  • You really shouldn't need to create an empty database to start with. Even if you specify a connection string as you have, if EF doesn't find a corresponding DB, it will just create one on the fly. The error you mentioned is a server access error, not an ef compiler error. Make sure you have proper permissions on the server and/or are running the app as an admin – Forty-Two Aug 01 '12 at 17:22

1 Answers1

0

It would seem that Entity Framework Code First deals with database model changes in two ways: one, it discards the old database and re-creates a new one; or two, it does not rest content until the user manually modifies it, as affirmed by @devdigital's link to EF 4.1 CF: CREATE DATABASE permission denied in database 'master'. Slauma's suggestion therein of using Database.SetInitializer(null); is good to ensure a production database is not dropped after creation, but does not create a table structure out of an empty database like I was trying to do. This being considered, my DBA granted me create access since he'd rather us not have to manually create every table, so it's not an issue anymore.

P.S. -- the database drop I mentioned came as a result of using one of the drop initialization strategies, probably DropCreateDatabaseAlways. Glad to know it was doing it's job.

Community
  • 1
  • 1
Bondolin
  • 2,793
  • 7
  • 34
  • 62