EF 代码优先通用实体和 EntityTypeConfiguration

EF Code first Generic Entity & EntityTypeConfiguration

我即将为我的实体创建通用 EntityEntityTypeConfiguration。这是我的课程:

IEntity

public interface IEntity<T>
{
    T Id { get; set; }
}

public interface IAuditableEntity<T>
{
    DateTime CreatedAt { get; set; }
    Membership.User CreatedBy { get; set; }
    int? CreatedById { get; set; }
    DateTime? DeletedAt { get; set; }
    Membership.User DeletedBy { get; set; }
    int? DeletedById { get; set; }
    T RevisionParentId { get; set; }
    bool isLastVersion { get; set; }
}

Entity.cs

public abstract class BaseEntity
{

}

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
    public virtual T Id { get; set; }
}

public abstract class AuditableEntity<T> : Entity<T>, IAuditableEntity<T>
{
    public DateTime CreatedAt { get; set; }
    public virtual Membership.User CreatedBy { get; set; }
    public int? CreatedById { get; set; }
    public DateTime? DeletedAt { get; set; }
    public virtual Membership.User DeletedBy { get; set; }
    public int? DeletedById { get; set; }
    public T RevisionParentId { get; set; }
    public bool isLastVersion { get; set; }
}

问题是当我尝试定义 AuditableEntity 的通用 EntityTypeConfiguration 时,因为:

public class AuditableEntityConfig<T> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T> where T : AuditableEntity<int>
{
    public AuditableEntityConfig()
    {
        HasOptional(x => x.CreatedBy).WithMany().HasForeignKey(x => x.CreatedById);
        HasOptional(x => x.DeletedBy).WithMany().HasForeignKey(x => x.DeletedById);
        Property(x => x.DeletedAt).IsOptional();
        Property(x => x.RevisionParentId).IsOptional();
    }
}

public class User : AuditableEntity<long>
{

}

你看我不得不 AuditableEntity<int> 这是错误的,我不知道要替换什么才能让它工作。 AuditableEntity<int> 应该类似于 AuditableEntity<T> 并且 T 可以是 string、int、guid、long、...

更新 按照迈克回答的建议,我进行了更改并更新了我的问题:

public class User : AuditableEntity<int>
{
    [Index("IX_uniqueUsername", IsUnique = true)]
    public string Username { get; set; }
    public string Password { get; set; }
    public string Email { get; set; }
    public bool Active { get; set; }
    public virtual List<Role> Roles { get; set; }
    public virtual List<UserGroup> Groups { get; set; }
    public virtual UserProfile Profile { get; set; }
    public bool isSuperAdmin { get; set; }
}

public class UserConfig : AuditableEntityConfig<User, int>
{
    public UserConfig()
    {
        ToTable("Account_Users");
        Property(x => x.Username).HasMaxLength(200).IsRequired();
        Property(x => x.Password).HasMaxLength(200).IsRequired();
        Property(x => x.Email).HasMaxLength(200);
        HasMany(x => x.Roles).WithMany(x => x.Users).Map(x =>
        {
            x.ToTable("Account_UserRoles");
            x.MapLeftKey("UserId");
            x.MapRightKey("RoleId");
        });
        HasMany(x => x.Groups).WithMany(x => x.Users).Map(x =>
        {
            x.ToTable("Account_UserGroups");
            x.MapLeftKey("UserId");
            x.MapRightKey("GroupId");
        });
    }
}

我现在收到 RevisionParentId 属性的错误:

The type 'TK' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Data.Entity.ModelConfiguration.Configuration.StructuralTypeConfiguration<TStructuralType>.Property<T>(System.Linq.Expressions.Expression<System.Func<TStructuralType,T>>)

这一行:

Property(x => x.RevisionParentId).IsOptional();

where T : AuditableEntity<T> 将导致递归类型检查。请尝试

已更新

public class AuditableEntityConfig<T, TK> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>
    where T : AuditableEntity<TK> where TK : struct { }