如何使用< Interface>类型的EntityCollection使用实体框架的部分类?
我正在使用部分类。一个类是从EntityFramework生成的。另一个类,我生成自己,以便我可以实现一个接口。该接口将被使用,所以两个库/程序集将不会彼此了解,但将能够实现通用接口。
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.
我的自定义界面声明属性EntityCollection( ITimesheetLabors ),但是我的EntityFramework生成EntityCollection(TimesheetLabors),所以编译器告诉我,我的类没有实现可以理解的EntityCollection(ITimesheetLabors)。然而,使我的部分类返回我想要的接口集合的最佳方法是什么?
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?
如果我的部分类的collection属性的获取返回一个新实例化的EntityCollection可以将具体的类型转换为我的接口?看起来有点过分了我缺少什么?
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;}
}
EF不支持接口,所以唯一的方法是使用由EF生成的原始属性和在内部访问生成的属性的接口属性。
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。你的设计闻起来 - 一方面你试图通过使用界面隐藏你的实体,同时你暴露了 EntityCollection
=>你正在使你的上层依赖于EF。常见的做法是做相反的设计 - 隐藏EF并使用POCO(通常不需要接口)。
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).