If I have a common interface implemented by all entities, for example:
public interface IEntity {
int Id { get; set; }
DateTime ValidFrom { get; set; }
DateTime? ValidTo { get; set; }
}
Is it at all possible to filter out objects returned as part of a query if ValidTo is null – only return still valid objects.
Where I get this idea from is that Julia Lerman and Rowan Miller use something similar in PEF:DbContext to set state on disconnected entities, however they do it via the state manager which allows them to iterate through all entities which implement a particular interface – I want to do something similar, but on a query which involves sub-entities.
I am using SQL Server 2012, EF5 and .Net 4.5.
The example you refer to relies on the
ChangeTracker.Entries<T>()method that accepts an interface as generic type parameter. Of course this all happens in memory, there is no translation to SQL involved.When it comes to translating linq to SQL, entity framework does not (yet) support interfaces, basically because
DbSet<T>must be based on a concrete class.Your only chance is to create a base type in stead of an interface and have your entities derive from that.