我正在使用MVVM模式创建一个多平台应用程序(.NET 4.5 / WPF,Windows Phone 8,SilverLight 5)。我的ViewModel项目是所有平台特定视图共享/使用的PCL(可移植类库)。在我的Windows应用程序中,我想直接通过Entity Framework 6访问数据库。
我有两个代表数据层的项目:
Data.Common:包括entity / repository / 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.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; }
}
如何在我的界面中表示数据表关系的集合(如ICollection<IUserApplication>
)是什么样的?我不可能简单地将Collection<IUserApplication>
转换为Collection<UserApplication>
,这样可以通过其他任何方式实现吗?