Reputation: 892
I have just published a website with a database to azure that has the code first migrations enabled. When I published the seed method in the Configuration.cs ran and seeded the db nicely as expected.
A couple weeks later I went back to the project and all of the seed data had been duplicated many times. I figure that somehow the seed method is being called multiple times while it is online.
Below is my Configuration.cs file which I think is fairly standard.
To fix it thus far I have just manually deleted all of the tables from the db on azure with SQL server, in the seed method I have changed .Add(c);
to .AddOrUpdate(c);
I have auto-migrations on, have created an initial-migration and am just about to re-update and publish.
Is there any other reason why the seed method may have run multiple times? I'm thinking something to do with dynamic resource allocation in the cloud......?
internal sealed class Configuration : DbMigrationsConfiguration<Trojan.Models.TrojanContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Trojan.Models.TrojanContext context)
{
GetCategories().ForEach(c => context.Categories.AddOrUpdate(c));
}
private static List<Category> GetCategories()
{
var categories = new List<Category> {
new Category
{
CategoryId = 1,
CategoryName = "Chip Life Cycle",
},
new Category
{
CategoryId = 2,
CategoryName = "Abstraction",
},
new Category
{
CategoryId = 3,
CategoryName = "Properties",
},
new Category
{
CategoryId = 4,
CategoryName = "Location",
},
};
return categories;
}
}
Upvotes: 0
Views: 147
Reputation: 1235
You are right, your website could be "sleeping" if not used. Also, in Azure Websites you don't have so much control over resources. The correct approach is to use the AddOrUpdate method.
Regards.
Upvotes: 1