Question

I am trying to publish a project that uses two DB connection strings and Migration Contexts:

DefaultConnection - Created automatically when starting the project and contains the user tables

AmscanContext - Generated when creating the Entity Model using Code First from an Existing database

I enabled and added two migrations for each one using a different folders and updated the databases (commenting out the create tables for the database I imported)

All works well locally and I have even added some authentication and canEdit rules to the controllers.

I have set up the connection strings to create two new databases and mentioned in the comment below. Here is the configuration.cs for each migration.

This is the App data:

namespace AMScan.Migrations.AmscanContext
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

internal sealed class Configuration : DbMigrationsConfiguration<AMScan.Models.AmscanContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\AmscanContext";
    }

    protected override void Seed(AMScan.Models.AmscanContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

}

This is the User data:

namespace AMScan.Migrations.ApplicationDbContext
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
internal sealed class Configuration :      DbMigrationsConfiguration<AMScan.Models.ApplicationDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        MigrationsDirectory = @"Migrations\ApplicationDbContext";
    }

    protected override void Seed(AMScan.Models.ApplicationDbContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );
        //
    }
}

}

Not sure if this tells you much. If there are any specific files you would like me to post please let me know.

Was it helpful?

Solution

I have managed to get this working. I took the following steps:

  1. Create new project and add the data entity model using Code First on Exiting Database.
  2. Create a Controller item
  3. Enable Migrations for each context using -MigrationsDirectory switch (see here for more info).
  4. Add each of the migrations and update the database.
  5. Publish the solution to azure creating a new website and TWO new databases making sure to check Execute Code First Migrations for both.
  6. Click Register and create an account (this creates the IdentityModel user database
  7. View the index of the controller you created (This created the app database)

This video was also very useful to learn about using code first on existing databases

I hope this will make sense any anyone looking for help. I am new to this and appreciate the help offered in this post and the other post I have read.

Cheers, Kevin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top