如何使用带有Entity Framework?</interface>的分部类来利用<interface>类型的EntityCollection

时间:2012-07-16 20:34:21

标签: c# entity-framework interface

我正在使用部分课程。一个类是从EntityFramework生成的。另一个我自己生成的类,以便我可以实现一个接口。将使用该接口,因此两个库/程序集将不会彼此了解,但将能够实现公共接口。

我的自定义接口声明属性EntityCollection( ITimesheetLabors )但我的EntityFramework生成EntityCollection(TimesheetLabors),因此编译器告诉我我的类没有实现可理解的EntityCollection(ITimesheetLabors)。但是,使我的partial类返回我想要的接口集合的最佳方法是什么?

我的partial类的我的collection属性的get是否应该返回一个新实例化的EntityCollection,以便它可以将具体类型转换为我的接口?这似乎有点矫枉过正。我错过了什么?

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;} 
}   

1 个答案:

答案 0 :(得分:1)

EF不支持接口,因此唯一的方法是使用EF生成的原始属性和您的内部访问生成的属性的接口属性。

顺便说一下。你的设计气味 - 一方面你试图通过使用界面隐藏你的实体,同时你暴露EntityCollection =&gt;你正在使你的上层依赖于EF。常见的方法是采用相反的设计 - 隐藏EF并使用POCO(通常不需要接口)。