public interface IEntity<TId>
{
TId Id { get; set; }
}
public abstract class Auditable
{
public int CreatedBy { get; set; }
public User CreatedUser { get; set; } = null!;
public DateTime Created { get; set; }
public int LastModifiedBy { get; set; }
public User ModifiedUser { get; set; } = null!;
public DateTime? LastModified { get; set; }
}
public class AccountStatus : Auditable, IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string? Code { get; set; }
public string? Description { get; set; }
}
public class AccountType : Auditable, IEntity<long>
{
public long Id { get; set; } ...
}
I am adding auditable base entity for existing Implemetation..when i inherited from Audtable entity I am getting error
A key cannot be configured on 'AccountStatus' because it is a derived type. The key must be configured on the root type 'Auditable'. If you did not intend for 'Auditable' to be included in the model, ensure that it is not referenced by a DbSet property on your context, referenced in a configuration call to ModelBuilder, or referenced from a navigation on a type that is included in the model.
Key may be int/long for other entity.
I cant move key(Id) to Base class because key may be int/long for other entity. How can I achieve this in EF Core 5? EDIT:
public class AuditableMap : IEntityTypeConfiguration<Auditable>
{
public void Configure(EntityTypeBuilder<Auditable> builder)
{
builder.Property(t => t.Created).HasColumnType("timestamp without time zone");
builder.Property(t => t.LastModified).HasColumnType("timestamp without time zone");
builder.HasOne(t => t.CreatedUser).WithMany().HasForeignKey(t => t.CreatedBy).OnDelete(DeleteBehavior.Restrict);
builder.HasOne(t => t.ModifiedUser).WithMany().HasForeignKey(t => t.LastModifiedBy).OnDelete(DeleteBehavior.Restrict);
}
}
public class AccountStatusMap : IEntityTypeConfiguration<AccountStatus>
{
public void Configure(EntityTypeBuilder<AccountStatus> builder)
{
builder.ToTable("account_status", schema: "public");
builder.HasKey(t =>t.Id);
builder.Property(t =>t.Name).HasMaxLength(100);
builder.Property(t => t.Code).HasMaxLength(10);
builder.Property(t =>t.Description).HasMaxLength(200);
}
}