我在數據庫(SQL Server 2008)中有奇怪的結果 - 。使用實體框架遷移數據庫
我使用Entity Framework處理ASP.NET MVC 3項目,並使用數據庫遷移。
當修改我數據庫的模式,並且在配置文件(Configuration.cs
)種方法中,我有以下代碼任何遷移後,初始化數據:
protected override void Seed(YAnnonce.Core.Repository.AnnonceDbContext context)
{
var categorieAnnonces = new List<CategorieAnnonce>
{
new CategorieAnnonce{ CategorieAnnonceID = 1, CategorieName="Emploi"},
new CategorieAnnonce{ CategorieAnnonceID = 2, CategorieName="Stage"},
};
categorieAnnonces.ForEach(a => context.CategorieAnnonces.AddOrUpdate(a));
context.Annonces.AddOrUpdate(new Annonce[10]{
new Annonce {AnnonceID=250, titre = "Offre d'emploi", CategorieAnnonce = categorieAnnonces[0], description="le cabinet de recrutement le pole offre à toute pe",date=DateTime.Now,etat=1, mode = 0, EndDate = DateTime.Now},
new Annonce {AnnonceID=490, titre = "Formation", CategorieAnnonce = categorieAnnonces[1], description="Le cabinet de recrutement et de formation maroc ",date=DateTime.Now,etat=0, mode = 1, EndDate = DateTime.Now},
new Annonce {AnnonceID=380,titre = "Freelance", CategorieAnnonce =categorieAnnonces[1], description="Bonjour, jeune développeur en informatique vous assurant ",date=DateTime.Now, etat=1, mode = 1, EndDate = DateTime.Now});
}
,但問題是,相同的數據在任何遷移後都會添加到數據庫中,而我不想要這種情況。
['AddOrUpdate'](http://msdn.microsoft.com/en-us/library/hh846520(V = vs.103)的.aspx)應該照顧這無論如何 - _」通過鍵添加或更新實體...等效於數據庫術語中的「upsert」操作「_ –