0

I created a visual studio 2012 MVC4 App. I am testing the "publish" functionality by right clicking the project and choosing publish. I followed the instructions here. I can connect to the remote web server and the folders get published to the correct folder, except the content folder for some reason.

When I run browse to the remote web server it prompts me for login so the app is working. However, the migrations never happened. The only tables created are the simplemembership tables, so I know the web server is connecting to the remote db server. No other tables are created and the seed method doesn't run. I seed the roles and a default user.

I checked the box in publish settings that says "Execute Code First Migrations (runs on application start)"

Everything works fine on my localdb connection string for local testing. Just can't figure out how to create db from existing migrations and seed when I publish to live site, note I will only seed once. Is there a way to specify which migrations to run again? I can copy the migrations and run on the database server but why the extra step?

EDIT: When adding the database.setinilizer to my context I now get an error saying some of my fields in my userprofile table are not there, I use simple membership. This error occurs on the first page load after web publish, then on proceeding page loads I get an error The "WebSecurity.InitializeDatabaseConnection" method can be called only once.

HOwever, it does create my simplemembership tables now but my migration for all other tables never runs, that is why I am missing the additional user profile fields.

EDIT: Basically I am not checking if websecurity is initialized prior to calling WebSecurity.InitializeDatabaseConnection so that resolved that issue. Now I have it partially working. The app creates the simplemembership tables fine but since I added tables to the UserProfile table I can't seed until I change them. So instead I manually create the userprofile table and have the app create the rest of the tables. Then I comment out the userprofile table in my initial migration. After this when I sign in it will then create the rest of my tables.

Open issue is how to get my database migration to run prior to the simplemembership initialization?

Xaxum
  • 3,545
  • 9
  • 46
  • 66

2 Answers2

2

To get migration work on remote server, you need to add use SetInitializer in you Context class first :

        static MyDatabaseContext()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyProjectContext, Migrations.Configuration>());
        }

And in you Migration Configuration you need to add this code :

public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            AutomaticMigrationDataLossAllowed = false;
        }

I don't select the "Execute Code First Migrations (runs on application start)", and just after setting initialization in MyProjectContext, it does the migration for me. If you have done by here, for seed your data, you can do same as below in your Migration configuration class:

      protected override void Seed(MyProject.Models.MyProjectContextcontext)
        {               
          DoSeed(context);               
        }

        private void DoSeed(MyProjectContext context)
        {
          var users = new List<User>
          {
            new Site { UserId = 1, Name = "TestUser", IsDeleted = false },
            new Site { UserId = 2, Name = "TestUser2", IsDeleted = false }
          };
            users.ForEach(s => context.Users.AddOrUpdate(s));
            context.SaveChanges();
}

I have not selected the "Execute Code First Migrations (runs on application start)" on deploy Profile. For some more details on this see this:

This link and This link

Hope this helps(as it worked for me), otherwise please add any error if there is, when you deploy you app.

InkHeart
  • 810
  • 1
  • 7
  • 17
  • Please check these links if you have not seen this already, I hope it can help you. [Link](http://blog.longle.net/2012/09/25/seeding-users-and-roles-with-mvc4-simplemembershipprovider-simpleroleprovider-ef5-codefirst-and-custom-user-properties/) and this one [Link](http://odetocode.com/Blogs/scott/archive/2012/08/31/seeding-membership-amp-roles-in-asp-net-mvc-4.aspx) – InkHeart May 10 '13 at 05:10
  • Thanks for the follow up. I already have that in place. I have no problems when running from a localdb on my dev machine. It is just when I try to push up to our test web server. I have to create the UserProfile table first so the other migrations will happen. For some reason my migration of the data tables only runs after I log into the site? – Xaxum May 10 '13 at 13:21
1

I think the issue is ,Because of the fact that as long as you have not tried to access data or create any data from/in site, which needs to connect to database, the migration and seeding will not run" And the reason for running migration on your site after logging into the site, would be because your site need to be authorised in all pages, or the page that you want to see data in. If you have a page example home page, that does not authorization to access to the page, you will see the page and if in this page there is some data that needs to be fetched from data base, you may see the migration runs. I found this from this Deploy it to IIS, but not sure if it is the reason. Please let me know if your migration still has not ran if you browse your home page that has data and no authentication needed for this data access.

InkHeart
  • 810
  • 1
  • 7
  • 17