Are generic classes not supported as models in Entity Framework?

I am trying to do something like this :

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

    public TrackerContext(bool trackNewValues = false)
        : base()
    {
        TrackNewValues = trackNewValues;
    }

    public TrackerContext(string connectinString, bool trackNewValues = false)
        : base(connectinString)
    {
        TrackNewValues = trackNewValues;
    }

    public DbSet<AuditLog<string>> AuditLog { get; set; }
    public DbSet<AuditLogChild> LogChildren { get; set; }
}



public class AuditLog<UserIdOrUserNameColumnType>
{
    public AuditLog()
    {
        Children = new List<AuditLogChild>();
    }

    [Key]
    public Guid AuditLogID { get; set; }

    public UserIdOrUserNameColumnType UserId { get; set; }

    [Required]
    public DateTimeOffset EventDateUTC { get; set; }
}

But I guess DbSet<AuditLog<string>> is not supported. I get this error:

Additional information: The type ‘TrackerEnabledDbContext.AuditLog`1[System.String]’ was not mapped.
Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation.
Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject.

Is there any ways I can implement public DbSet<AuditLog<string>> AuditLog { get; set; } ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x