user134363
user134363

Reputation:

How can I utilize EntityCollection of type <Interface> using a partial class with Entity Framework?

I'm using partial classes. One class is generated from EntityFramework. Another class I generate myself so that I can implement an interface. The interface will be used so two libraries/assemblies will not know about each other but will be able to implement the common interface.

My custom interface declares property EntityCollection(ITimesheetLabors) but my EntityFramework generates EntityCollection(TimesheetLabors) so the compiler tells me that my class doesn't implement EntityCollection(ITimesheetLabors) which is understandable. However, what is the best method of making my partial class return the collection of interfaces I desire?

Should the get of my collection property of my partial class return a newly instantiated EntityCollection so that it can cast the concrete types to my interface? It seems a bit of overkill. What am I missing?

public interface ITimesheet //My Interface
{
    EntityCollection<ITimesheetLabors> Timesheets {get;set;} 
}

public partial class Timesheet//Class generated by Entity Framework
{
    EntityCollection<TimesheetLabors> Timesheets {get;set;} 
}

public partial class Timesheet : ITimesheet  //My Partial Class that implements my interface
{
    EntityCollection<ITimesheetLabors> Timesheets {get;set;} 
}   

Upvotes: 1

Views: 702

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364359

EF doesn't support interfaces so the only way is using both original property generated by EF and your interface property which will internally access the generated property.

Btw. your design smells - on the one side you are trying to hide your entities by using interfaces and in the same time you are exposing EntityCollection => you are making your upper layer dependent on EF. The common approach is to do the opposite design - hide EF and use POCOs (interfaces are usually not needed).

Upvotes: 1

Related Questions