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
Daniel M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.