C# EF代码第一通用实体&;EntityTypeConfiguration
我将为我的实体创建一个通用的C# EF代码第一通用实体&;EntityTypeConfiguration,c#,asp.net,entity-framework,ef-code-first,C#,Asp.net,Entity Framework,Ef Code First,我将为我的实体创建一个通用的实体和实体类型配置。以下是我的课程: Entity public interface IEntity<T> { T Id { get; set; } } public interface IAuditableEntity<T> { DateTime CreatedAt { get; set; } Membership.User CreatedBy { get; set; } int? CreatedById {
实体
和实体类型配置
。以下是我的课程:
Entity
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; }
}
在这行中:Property(x => x.RevisionParentId).IsOptional();
其中T:AuditableEntity
将导致递归类型检查。请试一试
已更新
public class AuditableEntityConfig<T, TK> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>
where T : AuditableEntity<TK> where TK : struct { }
公共类AuditableEntityConfig:System.Data.Entity.ModelConfiguration.EntityTypeConfiguration
其中T:AuditableEntity,其中TK:struct{}
你到底想“工作”什么。如果您的实体类型配置是泛型类型T,那么数据库中映射到
RevisionParentId
的字段应该是什么数据类型?通过将此字段的类型设置为泛型,您试图实现什么目的?RevisionParentId
是同一实体的自引用外键。例如:我有一个Post
实体,它的Id
类型为T
。因此,RevisionParentId
应该是T
类型,以尝试我的更新解决方案。在AuditableEntity
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");
});
}
}
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();
public class AuditableEntityConfig<T, TK> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>
where T : AuditableEntity<TK> where TK : struct { }