0

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

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

我的部分类的集合属性的获取是否应该返回一个新实例化的 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;} 
}   
4

1 回答 1

1

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

顺便提一句。你的设计有味道——一方面你试图通过使用接口来隐藏你的实体,同时你正在暴露EntityCollection=>你正在让你的上层依赖于 EF。常见的做法是做相反的设计——隐藏 EF 并使用 POCO(通常不需要接口)。

于 2012-07-17T08:20:27.143 回答