简体   繁体   中英

seed method works in local machine but not appharbor

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插件和设置

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:

配置变量

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

配置页

That should probably do the trick.

Your connectionstring name should be the same as the alias ie "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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM