【问题标题】:Database migration with Entity Framework使用实体框架进行数据库迁移
【发布时间】:2012-11-15 14:33:35
【问题描述】:

我在数据库(SQL Server 2008)中有奇怪的结果-.

我在一个带有实体框架的 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});
}

但问题是任何迁移后都会将相同的数据添加到数据库中,我不希望出现这种情况。

【问题讨论】:

    标签: asp.net-mvc-3 sql-server-2008 c#-4.0 entity-framework-4.3


    【解决方案1】:

    您必须确定应该在哪个状态下使用数据库迁移。有不同的方法可以做到这一点,我建议其中一种。写一个这样的类,并把 Seed 函数放进去:

    public class DataContextInitializer:DropCreateDatabaseIfModelChanges<YAnnonce.Core.Repository.AnnonceDbContext>
    {
        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});
    }
    

    然后在您的 main/root web.config 文件中,添加以下键以在应用程序运行期间运行上述类:

    <appSettings>
        <add key="DatabaseInitializerForType YAnnonce.Core.Repository.AnnonceDbContext, [Project_Name]" value="DataContextInitializer, [Project_Name]" />
    </appSettings>
    

    【讨论】:

      【解决方案2】:

      插入前检查记录是否存在。 如果您考虑在单个事务中插入数据,那么您可以在插入之前搜索是否存在任何一条记录。 即

      if(!context.Annonces.Any(x=>x.AnnonceID==250)){ //insert records here }
      

      【讨论】:

      • AddOrUpdate 无论如何都应该解决这个问题 - “按键添加或更新实体...相当于数据库术语中的“upsert”操作”
      猜你喜欢
      • 2014-07-08
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多