gotmike
gotmike

Reputation: 1615

Entity Framework 6 Code First to RDS trying to Create Database

I have an existing database and also some existing code that I'd like to push to that database using the Entity Framework (v6.0.0.0).

I am having no problem connecting to the DB, and I have examined the context and migrations files, all look good to me.

When I run the Update-Database -Verbose command, I'm getting the following error:

CREATE DATABASE permission denied in database 'master'.

I'm not clear why it's even trying to create a database, b/c one already exists, and I'm specifying the DB in the settings.

Here is my app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxx" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
          <parameter value="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]"/>
  </connectionStrings>
</configuration>

Any reason it would be ignoring my DB name and trying to create a new one from scratch?

Upvotes: 0

Views: 1744

Answers (1)

gotmike
gotmike

Reputation: 1615

So I figured it out...

The solution (in my case at least) was that I had two connection strings in my app.config file.

I had one in the <connectionsStrings> section and another in the <parameters> section (see above).

At one point I even commented out the one in <connectionStrings> because the other one seemed to look more like an Entity Framework version.

As it turns out, I followed the advice I found in a few other answers regarding setting up an empty class in the DbContext file and hard-code it to the name listed in the <connectionStrings> section.

So like this:

public class YourContext : DbContext
    {
        public YourContext() : base("name=DefaultConnection")
        {

        }

        public DbSet<aaaa> Aaaas { get; set; }
    }

After I did that, I got the following error:

The connection string 'DataModel' in the application's configuration file does not contain the required providerName attribute."

So, I changed my connectionStrings to look like this:

  <connectionStrings>
    <add name="DataModel" connectionString="Server=[xxxxxxxxxx].rds.amazonaws.com,1433;Database=[xxxxxxxxxx];UID=[xxxxxxxxxx];PWD=[xxxxxxxxxx]" providerName="System.Data.SqlClient" />
  </connectionStrings>

And after that, I got the following error:

CREATE TABLE permission denied in database '[xxxxxxxxx]'.

So I went back to the DB and added the db_ddladmin Database Role for the DB in question.

After that, I ran Update-Database and it worked!

Of course, I went back in and removed the db__ddladmin permission and also the View any database server permission.

Upvotes: 1

Related Questions