Are generic classes not supported as models in Entity Framework?

后端 未结 2 908
遥遥无期
遥遥无期 2020-12-19 02:04

I am trying to do something like this :

public class TrackerContext : DbContext
{
    public bool TrackNewValues { get; set; }

    public TrackerContext(boo         


        
相关标签:
2条回答
  • 2020-12-19 02:51

    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.

    0 讨论(0)
  • 2020-12-19 03:00

    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
    0 讨论(0)
提交回复
热议问题