سؤال

My seed method works perfectly fine when running in my local machine, but when I push my code to appharbor the seed is not working.

This is my connection string:

<connectionStrings>
    <add name="FinalProjectContext" connectionString="Data Source=.\SQLEXPRESS;User
Id=user;Password=pass;initial catalog=MyContextDB;" 
providerName="System.Data.SqlClient" />
</connectionStrings>

My seed method is inside the Configuration file for migrations:

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

protected override void Seed(MyContext context)
{
   //add data
}

And I'm calling my seed method from the context class, inside the OnModelCreating:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}

EDIT

SQL Server Addon and Settings

هل كانت مفيدة؟

المحلول

After a lot of debugging, searching, testing and reading, the problem wasn't really related to appharbor per say. The project is using a DBFactory for the context, and this where the problem was occurring. In my local machine EF was calling the Initializer every time I started the application so the Database would get seeded, but in order for this to work in app harbor I had to set the Initializer to false (explicitly) in the DBFactory constructor.

EDIT

The problem goes even further. Not sure how EF is tracking this but I think that if I run the code locally then the Database gets created locally and EF flags the migration as "executed", so when I move the code to Appharborit doesn't get seeded. Only way I've found to solve this is to delete the database locally and then push it to Appharbor.

نصائح أخرى

The problem is probably that your connection-string isn't set by AppHarbor. I'm not entirely sure how to ensure that it does, but if you go to your configuration-page for the MSSQL-"addon" from here:

config variables

Then set's the "SQLSERVER_CONNECTION_STRING_ALIAS" to your connection-string-name (in your case "MyContext" here:

config-page

That should probably do the trick.

Your connectionstring name should be the same as the alias i.e. "FinalProjectContext"

<add name="FinalProjectContext" connectionString="Data Source=.\SQLEXPRESS;UserId=user;Password=pass;initial catalog=MyContextDB;" providerName="System.Data.SqlClient" />

Then appharbor will know to swap it out for the proper one :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top