我是MVC
和EF Core
的新手,并且一直在尝试播种一些数据。除了一张桌子外,我所有桌子的种子都正确播种,我头撞墙撞腻了。我什至没有运气就重新创建了整个应用程序。我确定我缺少明显的东西。
我的模型
public class AgreementType
{
public int AgreementTypeID { get; set; }
[Required]
[StringLength(100)]
public string Description { get; set; }
[DisplayName("Creation Date")]
public DateTime CreationDate { get; set; }
[StringLength(100)]
[DisplayName("Created By")]
public string CreatedBy { get; set; }
public Agreement Agreement { get; set; }
}
public class ContactType
{
public int ContactTypeID { get; set; }
[Required]
[StringLength(100)]
public string Description { get; set; }
[DisplayName("Creation Date")]
public DateTime CreationDate { get; set; }
[StringLength(100)]
[DisplayName("Created By")]
public string CreatedBy { get; set; }
public Contact Contact { get; set; }
}
public class Branch
{
public int BranchID { get; set; }
public string Name { get; set; }
[DisplayName("Creation Date")]
public DateTime CreationDate { get; set; }
[StringLength(100)]
[DisplayName("Created By")]
public string CreatedBy { get; set; }
public int IntermediaryID { get; set; }
public Intermediary Intermediary { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
}
我的上下文课程
public class BizDevHubContext : DbContext
{
public BizDevHubContext(DbContextOptions<BizDevHubContext> options) : base(options)
{
}
public DbSet<AgreementType> AgreementType { get; set; }
public DbSet<Branch> Branch { get; set; }
public DbSet<ContactType> ContactTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<AgreementType>().ToTable("AgreementType");
modelBuilder.Entity<Branch>().ToTable("Branch");
modelBuilder.Entity<ContactType>().ToTable("ContactType");
// One to One
modelBuilder.Entity<Agreement>()
.HasOne(b => b.AgreementType)
.WithOne(i => i.Agreement)
.HasForeignKey<Agreement>(b => b.AgreementTypeID);
modelBuilder.Entity<Contact>()
.HasOne(b => b.ContactType)
.WithOne(i => i.Contact)
.HasForeignKey<Contact>(b => b.ContactTypeID);
//One to Many
modelBuilder.Entity<Region>()
.HasMany(c => c.Branch)
.WithOne(e => e.Region)
.HasForeignKey((p => p.BranchID));
// Set default dates
modelBuilder.Entity<AgreementType>()
.Property(b => b.CreationDate)
.HasDefaultValueSql("getdate()");
modelBuilder.Entity<Branch>()
.Property(b => b.CreationDate)
.HasDefaultValueSql("getdate()");
modelBuilder.Entity<ContactType>()
.Property(b => b.CreationDate)
.HasDefaultValueSql("getdate()");
}
}
我的初始化器类
if (context.Branch.Any())
{
return;
}
var branches = new Branch[]
{
new Branch{Name="Bloemfontein",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Cape Town",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Durban",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Nelspruit",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Johannesburg",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Port Shepstone",CreationDate=DateTime.Now,CreatedBy="System"},
new Branch{Name="Pretoria",CreationDate=DateTime.Now,CreatedBy="System"}
};
foreach (Branch b in branches)
{
context.Branch.Add(b);
}
context.SaveChanges();
除“分支”表外,其他所有表都是种子。
请帮助!