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?
The base entity is marked as an abstract class because you will never need to instantiate it. It simply serves as a base class for your actual entities. public abstract class BaseEntity<T> : IEntity.
Entity Framework Classic Include The Include method lets you add related entities to the query result. In EF Classic, the Include method no longer returns an IQueryable but instead an IncludeDbQuery that allows you to chain multiple related objects to the query result by using the AlsoInclude and ThenInclude methods.
It is a data access framework which used to create and test data in the visual studio. It is part of . NET Framework and Visual Studio.
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; }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With