C# 如何在通用接口中定义集合以适应EntityFrameworks自动生成的集合?
我正在使用MVVM模式创建一个多平台应用程序(.NET 4.5/WPF、Windows Phone 8、SilverLight 5)。我的ViewModel项目是一个由所有平台特定视图共享/使用的PCL(可移植类库)。在我的Windows应用程序中,我希望通过Entity Framework 6直接访问数据库 我有两个代表数据层的项目:C# 如何在通用接口中定义集合以适应EntityFrameworks自动生成的集合?,c#,entity-framework,mvvm,entity-framework-6,portable-class-library,C#,Entity Framework,Mvvm,Entity Framework 6,Portable Class Library,我正在使用MVVM模式创建一个多平台应用程序(.NET 4.5/WPF、Windows Phone 8、SilverLight 5)。我的ViewModel项目是一个由所有平台特定视图共享/使用的PCL(可移植类库)。在我的Windows应用程序中,我希望通过Entity Framework 6直接访问数据库 我有两个代表数据层的项目: Data.Common:包括实体/存储库/unitofwork接口。它们在我的ViewModels中使用。 Data.EF:包括EntityFramework“
Data.Common:包括实体/存储库/unitofwork接口。它们在我的ViewModels中使用。
Data.EF:包括EntityFramework“stuff”(edmx文件=自动生成的DbContext和实体类) 我将显示我的
IUser
界面的问题:
// Data.Common.IUser
public interface IUser : IEntity
{
// Primitives representing table columns.
// They will be implemented fine by EF's auto generated classes.
int ID { get; set;}
string UserName { get; set; }
// Collections representing table relations.
// They are my problem because EF's generated classes don't fit to this.
ICollection<IUserApplication> UserApplications { get; set; }
}
//Data.Common.IUser
公共接口IUser:IEntity
{
//表示表列的原语。
//它们将由EF的自动生成类很好地实现。
int ID{get;set;}
字符串用户名{get;set;}
//表示表关系的集合。
//它们是我的问题,因为EF生成的类不适合这个。
ICollection用户应用程序{get;set;}
}
为了实现我的接口,我在Data.EF项目中为每个EF自动生成的实体类创建了一个分部类:
// Data.EF.User, my file
public partial class User : IUser
{
// fields are implemented in other part
}
// Data.EF.User, file created by EntityFramework
public partial class User
{
public User()
{
this.UserApplications = new HashSet<UserApplication>();
}
public int ID { get; set; } // fine, this implements IUser.ID
public string UserName { get; set; } // fine, this implements IUser.UserName
// And here's the other part of my problem:
// This collection is of type ICollection<UserApplication> but UserApplication is not known in my ViewModel, only IUserApplication.
public virtual ICollection<UserApplication> UserApplications { get; set; }
}
//Data.EF.User,我的文件
公共部分类用户:IUser
{
//字段在其他部分中实现
}
//Data.EF.User,由EntityFramework创建的文件
公共部分类用户
{
公共用户()
{
this.UserApplications=new HashSet();
}
public int ID{get;set;}//很好,它实现了IUser.ID
公共字符串用户名{get;set;}//很好,它实现了IUser.UserName
//这是我问题的另一部分:
//此集合的类型为ICollection,但在我的ViewModel中不知道UserApplication,只有IUserApplication。
公共虚拟ICollection用户应用程序{get;set;}
}
在我的界面中表示数据表关系的集合(如ICollection
)应该是什么样子?不可能简单地将集合
强制转换为集合
,那么这可以通过任何其他方式实现吗