Are generic classes not supported as models in Entity Framework?
You cannot map the generic type because Entity Framework simply doesn't support generic Entity types. When using the EF Code-First approach you need to remember that you should model your POCO classes within the constraints that allow Entity Framework to create POCO proxies.
This means, shortly speaking that such a class:
- Should not contain any attributes
- Should not be generic
- Should be public
- Must not be sealed
- Must not be abstract
- Must have a public or protected constructor that does not have parameters
I have been using generic classes with success in Entity Framework.If you declare your class and DbSet the following way it will work.
public class AuditLogString : AuditLog<String>{}public DbSet<AuditLogString> AuditLogStrings { get;set;}
[Update]I have not used this method recently and in the light of the comments on this answer I suggest Pawel's answer instead.However I have not deleted this answer since I was able to use the method.