Bonjour,
Je me mets à jour avec mvc4 et tente donc d'utiliser la méthode seed() pour pré remplir ma db. le problème est que à aucun moment il ne rentre dans la méthode.
voici mon context:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public class ErpContext : DbContext
{
public ErpContext(): base("test")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Profil> Profils { get; set; }
public DbSet<Team> Teams { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<ErpContext>(new ErpInitializer());
}
} |
mon initializer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| public class ErpInitializer: DropCreateDatabaseAlways<ErpContext>
{
protected override void Seed(ErpContext context)
{
/**
* initialize teams table
*/
List<Team> listTeam = new List<Team>();
listTeam.Add(new Team{name="Belgique"});
listTeam.Add(new Team { name = "Espagne" });
listTeam.Add(new Team { name = "Italie" });
listTeam.Add(new Team { name = "Brésil" });
try
{
listTeam.ForEach(s => context.Teams.Add(s));
context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
} |
et pour finir voici mon Application_Start()
Database.SetInitializer(new ErpInitializer());
Merci de votre aide
Partager