不是第一个数据库,但是遇到了问题,看不懂。
模型:
public class TemplateWork
{
public int Id { get; set; }
/// <summary>
/// Родительский объект
/// </summary>
public int? ParentId { get; set; }
public TemplateWork? Parent { get; set; }
public string Name { get; set; } = string.Empty;
public IEnumerable<TemplateWork> Stages { get; set; }
public TemplateWork() => Stages = new List<TemplateWork>();
}
型号配置:
public class TemplateWorkEFConfiguration : IEntityTypeConfiguration<TemplateWork>
{
public void Configure(EntityTypeBuilder<TemplateWork> builder)
{
builder.ToTable("TemplateWorks");
builder
.HasOne(o => o.Parent)
.WithMany(s => s.Stages)
.HasForeignKey(k => k.ParentId)
.OnDelete(DeleteBehavior.SetNull);
}
}
BD上下文
public class ApplicationDBContext : DbContext
{
public ApplicationDBContext()
{
Database.EnsureCreated();
}
public DbSet<AppUser> AppUsers { get; set; } = null!;
public DbSet<TemplateWork> TemplateWorks { get; set; } = null!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=.............");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new TemplateWorkEFConfiguration());
}
}
未创建表。此外,当我包含公共 DbSet TemplateWorks 时,没有创建表,没有它一切正常。执行时不抛出异常。其他连接模型(2张桌子上的真相) - 解决。帮助我理解......