我在Asp.NetCore中实现了EntityFrameworkCore,但是下面的代码无法在SQL Express中生成表,它只生成身份表和文档,但没有Standard
和Form
。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Standard> Standard { get; set; }
public DbSet<Form> Form { get; set; }
public DbSet<MasterDocument> Document { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
//builder.Entity<Standard>().ToTable("Standard");
//builder.Entity<Form>().ToTable("Form");
//builder.Entity<MasterDocument>().ToTable("Document");
builder.Entity<Form>()
.HasOne(f => f.Standard)
.WithMany(s => s.Forms)
.HasForeignKey(k => k.StandardId)
.OnDelete(DeleteBehavior.Restrict);
}
}
模型
public class MasterDocument
{
public int ID { get; set; }
[Required]
[Display(Name = "EStandard")]
[StringLength(50)]
public string EStandard { get; set; }
[Required]
[Column("Betegnelse")]
[Display(Name = "Betegnelse")]
[StringLength(60)]
public string Betegnelse { get; set; }
[Display(Name = "Kommentar")]
public string Comment { get; set; }
}
public class Standard : MasterDocument
{
[Display(Name = "Dokumenter")]
public ICollection<Form> Forms { get; set; }
}
public class Form : MasterDocument
{
public String Status { get; set; }
public int StandardId { get; set; }
//[ForeignKey("StandardId")]
public Standard Standard { get; set; }
}
任何帮助都将不胜感激。