Base class/Entity in EntityFramework 5.0

asp.net-mvc, asp.net-mvc-4, c#, ef-database-first, entity-framework-5

Solution

Don't create a base class. Create an Interface, like below:

public interface IMyEntity
{
    DateTime CreatedAt { get; set; }
    string CreatedBy { get; set; }
    // Other properties shared by your entities...
}

Then, your Models will be like this:

[MetadataType(typeof(MyModelMetadata))]
public partial class MyModel : IMyEntity
{
   [Bind()]  
   public class MyModelMetadata
   {
      [Required]
      public object MyProperty { get; set; }

      [Required]
      public string CreatedBy { get; set; }  
   }
}

Problem

I'm using Entity Framework 5 in Database First approach and I am using edmx file. Most of my entities have 6 common fields. Fields like `CreatedAt`, `CreatedBy` etc. Now, I implemented some functions as extensions that can only be applied to `IQueryable` of those Entities that have the common fields. But when I implement the extension method, it can be accessed by any type of `IQueryable` as it's typed T and I can only define that the type T should always be of one type. So, I thought I can give a base class to the entities which has common fields and define type T as that base type. But, it seems I can't do this. Any idea on how to solve this or implement what I have explained above?

Original source

Related problems