0

I've got a Web API 2 project set up using Code First. The publish dialog was detecting the DB and allowing me to use Code First Migrations.

I then added a second DB connection using the steps here.

Now when I publish, the dialog only detects the new database and does not give me any options for the original one.

I removed every reference to the second DB that I can find, but the Publish dialog is still showing references to it (and not to the original DB). I've compared my current revision to the one before I added the second DB, and the pubxml for my publishing profile is different, but VS auto-generates the part that is different:

Before:

<PublishDatabaseSettings>
  <Objects>
    <ObjectGroup Name="DefaultConnection" Order="1" Enabled="True">
      <Destination Path="" />
      <Object Type="DbCodeFirst">
        <Source Path="DBMigration" DbContext="Project.Models.ApplicationDbContext, Project" MigrationConfiguration="Project.Migrations.Configuration, Project" Origin="Convention" />
      </Object>
    </ObjectGroup>
  </Objects>
</PublishDatabaseSettings>
...
<ItemGroup>
  <MSDeployParameterValue Include="$(DeployParameterPrefix)DefaultConnection-Web.config Connection String">
    <ParameterValue>Data Source=localhost;Initial Catalog=OriginalDB;User ID=user;Password=password</ParameterValue>
  </MSDeployParameterValue>
</ItemGroup>
<ItemGroup>
  <_ConnectionStringsToInsert Include="DefaultConnection" />
</ItemGroup>

After:

<PublishDatabaseSettings>
  <Objects>
    <ObjectGroup Name="NewDatabase" Order="2" Enabled="False" xmlns="">
      <Destination Path="" />
      <Object Type="DbDacFx">
        <PreSource Path="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=newcatalog;Integrated Security=True;Application Name=EntityFramework" includeData="False" />
        <Source Path="$(IntermediateOutputPath)AutoScripts\NewDatabase_IncrementalSchemaOnly.dacpac" dacpacAction="Deploy" />
      </Object>
      <UpdateFrom Type="Web.Config">
        <Source MatchValue="data source=(localdb)\MSSQLLocalDB;initial catalog=newcatalog;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" MatchAttributes="$(UpdateFromConnectionStringAttributes)" />
      </UpdateFrom>
    </ObjectGroup>
  </Objects>
</PublishDatabaseSettings>
...
<ItemGroup>
  <MSDeployParameterValue Include="$(DeployParameterPrefix)NewDatabase-Web.config Connection String" />
</ItemGroup>

I'm not sure where this is being generated from, how would I go about getting this to work with both databases, or failing that, getting it back to the point where it works with the original DB?

Digital-Clouds
  • 532
  • 4
  • 16
  • What do you have for connection strings in your Web.config and are you using these in your context constructor? – Steve Greene Jul 05 '16 at 13:32
  • Looking at the "before" version (since I've messed around with the new one quite a lot in an effort to get it to work again), there aren't any connection strings in there, but there is a `defaultConnectionFactory` element (not sure if it's related). Original Context constructor is: `public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)`, which matched the connection strings that used to be shown when deploying. – Digital-Clouds Jul 05 '16 at 13:42
  • I would use an explicit connection string rather than relying on defaults. See https://msdn.microsoft.com/en-us/data/jj592674.aspx – Steve Greene Jul 05 '16 at 13:44
  • Have converted it to use that, and it is now shown in the Publish dialog, but the Code First Migrations check box isn't there any more. Does something special need putting in the connection string to get this to be available? – Digital-Clouds Jul 06 '16 at 08:15

0 Answers0