Reputation: 1140
I'm about to create a Generic Entity
and EntityTypeConfiguration
for my entities. here are my classes:
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; }
}
The problem is when I try to define a generic EntityTypeConfiguration of AuditableEntity, because :
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>
{
}
You see I had to AuditableEntity<int>
which is wrong and I have no idea what to replace to get it work.
AuditableEntity<int>
should be something like AuditableEntity<T>
and T can be string, int, guid, long, ...
UPDATE
as suggested by Mike answer, I made changes and updated my question:
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");
});
}
}
I get this error now for the RevisionParentId
property:
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>>)
in this line:
Property(x => x.RevisionParentId).IsOptional();
Upvotes: 2
Views: 1495
Reputation: 4051
where T : AuditableEntity<T>
will cause recursive type checking. Please try
UPDATED
public class AuditableEntityConfig<T, TK> : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<T>
where T : AuditableEntity<TK> where TK : struct { }
Upvotes: 2