EF Code第一个通用实体& EntityTypeConfiguration

时间:2015-01-05 09:38:50

标签: c# asp.net entity-framework ef-code-first

我即将为我的实体创建一个通用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,......

更新 正如Mike回答的那样,我做了更改并更新了我的问题:

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

1 个答案:

答案 0 :(得分:2)

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

<强>已更新

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