C# dbcontext configuration multiple entities mapping to the same base entity and table with different query filters

I’m trying to map multiple entities that inherit from the same entity to different dbsets but live on the same table

for example I have the following entity, where base entity simply defines common fields i want in all tables of my db

public class BaseEntity
{
    public int Id { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime UpdatedOn { get; set; }
}

public class RegistryEntity : BaseEntity
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string? CompanyName { get; set; }
    public string? LegalName { get; set; }
    public string Denomination { get; set; } = String.Empty;
    public bool IsCustomer { get; set; }
    public bool IsSupplier { get; set; }
    public bool IsPerson { get; set; }
    public bool IsCorporation { get; set; }
}

public class CustomerEntity : RegistryEntity
{
}

public class SupplierEntity : RegistryEntity
{
}

With the folllowing configurations

public class BaseEntityConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : BaseEntity
{
    public virtual void Configure(EntityTypeBuilder<TEntity> builder)
    {
        builder.HasKey(b => b.Id);
        builder.Property(b => b.CreatedOn).HasDefaultValue(DateTime.UtcNow).ValueGeneratedOnAdd();
        builder.Property(b => b.UpdatedOn).HasDefaultValue(DateTime.UtcNow).IsConcurrencyToken();
    }
}

public class RegistryEntityConfiguration : BaseEntityConfiguration<RegistryEntity>
{
    public override void Configure(EntityTypeBuilder<RegistryEntity> builder)
    {
        builder.ToTable("registries");
        base.Configure(builder);
    }
}

And I’d like to have SupplierEntity mapping to registries table where IsSupplier is true, same thing with Customer where IsCustomer is true, I’d also like to add CorporationEntity with a subset of
the columns of the registry table.

This is probably answered so many times but I can’t seem to make a specific solution for this.

If it may be possible I’d like for Suppliers and Customers to have only Denomination and IsCorporation and IsPerson

I ‘ve tried with multiple sort of the following code

public class SupplierEntityConfiguration : BaseEntityConfiguration<SupplierEntity>
{
    public override void Configure(EntityTypeBuilder<SupplierEntity> builder)
    {
        builder.ToTable("registries")
            .HasQueryFilter(s => s.IsSupplier);
    }
}

public class CustomerEntityConfiguration : BaseEntityConfiguration<CustomerEntity>
{
    public override void Configure(EntityTypeBuilder<CustomerEntity> builder)
    {
        builder.ToTable("registries")
            .HasQueryFilter(s => s.IsCustomer);
    }
}

But it kwwps giving me the following error when trying to add a migration

Unable to create a ‘DbContext’ of type ”. The exception ‘The filter expression ‘c => c.IsCustomer’ cannot be specified for entity type ‘CustomerEntity’. A filter may only be applied to the root entity type ‘RegistryEntity’.’ was thrown while attempting to create an instance. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

New contributor

Daniel M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật