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; } ...
}
我正在为现有的Implemetation添加可审核的基本实体..当我从Audtable实体继承时,出现错误
无法在“ AccountStatus”上配置密钥,因为它是派生类型。必须在根类型“可审核”上配置密钥。如果您不打算在模型中包含“可审核”,请确保您的上下文中的DbSet属性未引用它,在ModelBuilder的配置调用中未引用它,或在包含在其中的类型的导航中未引用它该模型。
对于其他实体,密钥可以是int / long。
我无法将key(Id)移至基类,因为其他实体的键可能是int / long。如何在EF Core 5中实现这一目标?编辑:
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);
}
}
如果Auditable
没有逻辑,而只是一个简单的数据类,则可以将其更改为接口:
public interface IAuditable
{
int CreatedBy { get; set; }
User CreatedUser { get; set; }; //I removed the null! here if need be, you might consider using Nullable-Types
DateTime Created { get; set; }
int LastModifiedBy { get; set; }
User ModifiedUser { get; set; };
DateTime? LastModified { get; set; }
}
public class AccountStatus : IAuditable, IEntity<int>
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string? Code { get; set; }
public string? Description { get; set; }
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 AccountType : IAuditable, IEntity<long>
{
public long Id { get; set; } ...
//add all fields from the interface IAuditable here as well
}
由于您必须在每个派生类中键入这些字段,因此增加了更多的仪式,但是许多IDE支持为实现的接口自动生成代码。
如果您有许多需要实现此接口的类,但是具有不同的ID数据类型,则可以这样创建基类:
public class AuditableIdInt: IAuditable
{
public int Id { get; set; }
// all fields from the Interface
}
public class AuditableIdLong: IAuditable
{
public long Id { get; set; }
// all fields from the Interface
}
您甚至可能会考虑泛型基类,其中泛型定义了ID类型。但我不确定这是否适用于EF。
PS:我怀疑AccountType是否需要一个长ID。检查整数类型列表
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句