EF Code first Generic Entity & EntityTypeConfiguration

本文关键字:amp EntityTypeConfiguration Entity Code first Generic EF | 更新日期: 2023-09-27 18:36:26

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

目录

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; }
}

实体.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; }
}

问题是当我尝试定义可审计实体的通用实体类型配置时,因为:

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 可以是字符串、整数、指导、长整型、...

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

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();

EF Code first Generic Entity & EntityTypeConfiguration

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

更新

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