私はEF7の移行を使用しようとしており、継承を使用して組織モデルをモデル化したときに固執しました。
組織は抽象クラスであり、それを継承する2つの具体的なクラスがIndividualおよびCompanyと呼ばれています。
DbContextでOrganization抽象クラスをDbSetとして設定し、移行を実行します。
次のエラーが表示されます。
エンティティタイプ 'Organization'の対応するCLRタイプはインスタンス化できず、モデルには具体的なCLRタイプに対応する派生エンティティタイプがありません。
私は何をすべきですか?
編集 - コードで更新
組織
public abstract class Organization
{
public Organization()
{
ChildOrganizations = new HashSet<Organization>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public bool Enabled { get; set; }
public bool PaymentNode { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
// virtual
public virtual ICollection<Organization> ChildOrganizations { get; set; }
}
個人
public class Individual : Organization
{
public string SocialSecurityNumber { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
}
会社
public class Company : Organization
{
public string Name { get; set; }
public string OrganizationNumber { get; set; }
}
DbContext
public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Organization> Organization { get; set; }
public CoreDbContext(DbContextOptions<CoreDbContext> options)
: base(options)
{
}
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);
}
}
前もって感謝します!
下記を参照してください。https : //docs.microsoft.com/en-us/ef/core/modeling/inheritance
階層内の1つまたは複数のエンティティのDbSetを公開したくない場合は、Fluent APIを使用してモデルに含まれるようにすることができます。
あなたが作成する必要がしたくない場合はDbSet
各サブクラスのために、あなたは明示的にそれらを定義する必要がありOnModelCreating
のオーバーライドDbContext
:
public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Organization> Organization { get; set; }
public CoreDbContext(DbContextOptions<CoreDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Individual>();
builder.Entity<Company>();
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);
}
}
リンクしたチュートリアルと同様に、 DbSet<>
プロパティは、継承するIndividual
クラスとCompany
クラスでなければなりません。
あなたのCoreDbContext
を以下のように見せてください:
public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Company> Companies { get; set; }
public DbSet<Individual> Individuals { get; set; }
public CoreDbContext(DbContextOptions<CoreDbContext> options)
: base(options)
{
}
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);
}
}