Вопрос

I have strange result in database (SQL Server 2008)-.

I work on a ASP.NET MVC 3 project with Entity Framework, and I use database migrations.

When I modify the schema of my database, and in configuration file (Configuration.cs) seed method, I have the following code to initialize data after any migration :

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});
}

but the problem is that the same data is added to the database after any migration, and I don't want this scenario.

Это было полезно?

Решение

You must determine in which state database migration should be used.There are different ways to do this, and i suggest one of them. Write a class like this and put Seed function into it :

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});
}

and then in your main/root web.config file, add below key to run the above class during the application run:

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

Другие советы

Check whether the records exists before inserting. If you consider the data to be inserted in a single transaction, then you can search if any one of the records exists before inserting. i.e

if(!context.Annonces.Any(x=>x.AnnonceID==250)){ //insert records here }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top