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.
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.