0
Ich verwende Code erste Migration Ansatz und Probleme mit dem Daten füllen. Datenbank/Tabellen werden auf Befehl update-database
erstellt, aber Daten werden nicht eingefügt.Entity Framework Seed-Methode funktioniert nicht
Hier Code
internal sealed class Configuration : DbMigrationsConfiguration<webapp_sample.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
//ContextKey = "webapp_sample.Models.ApplicationDbContext";
}
protected override void Seed(webapp_sample.Models.ApplicationDbContext context)
{
context.Companies.AddOrUpdate(p => p.Name,
new Models.Company { CompanyId = 1, Name = "ABC Traders" },
new Models.Company { CompanyId = 2, Name = "XYZ Traders" }
);
context.Divisions.AddOrUpdate(p => p.Name,
new Models.Division { CompanyId = 1, DivisionId = 1, Name = "IT" },
new Models.Division { CompanyId = 1, DivisionId = 2, Name = "Purchasing" },
new Models.Division { CompanyId = 1, DivisionId = 3, Name = "Finance" },
new Models.Division { CompanyId = 1, DivisionId = 4, Name = "Production" },
new Models.Division { CompanyId = 1, DivisionId = 5, Name = "Retail" },
new Models.Division { CompanyId = 2, DivisionId = 6, Name = "HR" },
new Models.Division { CompanyId = 2, DivisionId = 7, Name = "Cafe" },
new Models.Division { CompanyId = 2, DivisionId = 8, Name = "Projects" }
);
context.SubDivisions.AddOrUpdate(p => p.Name,
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 1, Name = "Application" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 2, Name = " Infra" },
new Models.SubDivision { CompanyId = 1, DivisionId = 1, SubDivisionId = 3, Name = "MIS" },
new Models.SubDivision { CompanyId = 1, DivisionId = 2, SubDivisionId = 1, Name = "Purchasing" },
new Models.SubDivision { CompanyId = 1, DivisionId = 3, SubDivisionId = 1, Name = "Finance" },
new Models.SubDivision { CompanyId = 1, DivisionId = 4, SubDivisionId = 1, Name = "Production" },
new Models.SubDivision { CompanyId = 1, DivisionId = 5, SubDivisionId = 1, Name = "Retail" },
new Models.SubDivision { CompanyId = 2, DivisionId = 5, SubDivisionId = 1, Name = "Admin" },
new Models.SubDivision { CompanyId = 2, DivisionId = 6, SubDivisionId = 1, Name = "Cafe" },
new Models.SubDivision { CompanyId = 2, DivisionId = 7, SubDivisionId = 1, Name = "Projects" }
);
}
}
und ApplicationDbContext
Datei wie unten
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<ApplicationUser>().Property(a => a.Latitude).HasPrecision(18, 9);
modelBuilder.Entity<ApplicationUser>().Property(a => a.Longitude).HasPrecision(18, 9);
base.OnModelCreating(modelBuilder);
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Company> Companies { get; set; }
public DbSet<Division> Divisions { get; set; }
public DbSet<SubDivision> SubDivisions { get; set; }
public System.Data.Entity.DbSet<webapp_sample.Models.MainMenu> MainMenus { get; set; }
}
Sie rufen SaveChanges nirgends auf. –