我正在使用Entity Framework 6在Visual Studio 2015中构建一个WPF MVVM Light应用程序,并使用数据库优先模型生成。 我正在为我的模型创建View Model类以与我的Views(用户控件)一起使用。
我有以下三种EF型号; Employee可以拥有许多EmployeeStatu而EmployeeStatu在ValidEmployeeStatu模型上有相应的description ('Leave of Absence','Archived'等)。 因此employeeID在EmployeeStatu上是FK,而且validEmployeeStatusID上的validEmployeeStatusID是FK( ValidEmployeeStatu上的PK)。
public partial class Employee { public Employee() { this.EmployeeStatus = new HashSet<EmployeeStatu>(); } public int employeeID { get; set; } // More properties here... public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; } } public partial class EmployeeStatu { public int employeeStatusID { get; set; } public int employeeID { get; set; } public int validEmployeeStatusID { get; set; } // More properties here... public virtual Employee Employee { get; set; } public virtual ValidEmployeeStatu ValidEmployeeStatu { get; set; } } public partial class ValidEmployeeStatu { public ValidEmployeeStatu() { this.EmployeeStatus = new HashSet<EmployeeStatu>(); } public int validEmployeeStatusID { get; set; } public string description { get; set; } // More properties here... public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; } }这是我的员工视图模型; 其他人遵循相同的模式:
public class EmployeeViewModel : ViewModelBase { private int _employeeId; private string _securityId; private IEnumerable<EmployeeStatusViewModel> _employeeStatus; public EmployeeViewModel () { this.EmployeeStatusVMs = new HashSet<EmployeeStatusViewModel>(); } public IEnumerable<EmployeeStatusViewModel> EmployeeStatusVMs { get { return _employeeStatus; } set { if (_employeeStatus.Equals(value)) return; _employeeStatus = value; RaisePropertyChanged(); } } // More properties here... }我正在使用此LINQ查询来获取数据:
var query = (from e in Context.Employees .Include("EmployeeStatus.ValidEmployeeStatu") .Where(comparison) select new EmployeeViewModel { employeeID = e.employeeID, securityID = e.securityID, firstName = e.firstName, middleName = e.middleName, lastName = e.lastName, suffix = e.suffix, job = e.job, organizationalUnit = e.organizationalUnit, costCenter = e.costCenter, notes = e.notes, createdDate = e.createdDate });如何将导航属性EmployeeStatu和ValidEmployeeStatu到我的View Models? 那么如何从SaveChanges()的View Model对象中将值返回到模型? 谢谢。
I'm building a WPF MVVM Light app in Visual Studio 2015 using Entity Framework 6 with database-first model generation. I'm creating View Model classes for my models to use with my Views (user controls).
I've got the following three EF models; an Employee can have many EmployeeStatu and an EmployeeStatu has a corresponding description ('Leave of Absence', 'Archived', etc.) on the ValidEmployeeStatu model. So employeeID is FK on EmployeeStatu and validEmployeeStatusID is FK on EmployeeStatu (PK on ValidEmployeeStatu).
public partial class Employee { public Employee() { this.EmployeeStatus = new HashSet<EmployeeStatu>(); } public int employeeID { get; set; } // More properties here... public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; } } public partial class EmployeeStatu { public int employeeStatusID { get; set; } public int employeeID { get; set; } public int validEmployeeStatusID { get; set; } // More properties here... public virtual Employee Employee { get; set; } public virtual ValidEmployeeStatu ValidEmployeeStatu { get; set; } } public partial class ValidEmployeeStatu { public ValidEmployeeStatu() { this.EmployeeStatus = new HashSet<EmployeeStatu>(); } public int validEmployeeStatusID { get; set; } public string description { get; set; } // More properties here... public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; } }Here's my View Model for Employee; the others follow the same pattern:
public class EmployeeViewModel : ViewModelBase { private int _employeeId; private string _securityId; private IEnumerable<EmployeeStatusViewModel> _employeeStatus; public EmployeeViewModel () { this.EmployeeStatusVMs = new HashSet<EmployeeStatusViewModel>(); } public IEnumerable<EmployeeStatusViewModel> EmployeeStatusVMs { get { return _employeeStatus; } set { if (_employeeStatus.Equals(value)) return; _employeeStatus = value; RaisePropertyChanged(); } } // More properties here... }I'm using this LINQ query to fetch the data:
var query = (from e in Context.Employees .Include("EmployeeStatus.ValidEmployeeStatu") .Where(comparison) select new EmployeeViewModel { employeeID = e.employeeID, securityID = e.securityID, firstName = e.firstName, middleName = e.middleName, lastName = e.lastName, suffix = e.suffix, job = e.job, organizationalUnit = e.organizationalUnit, costCenter = e.costCenter, notes = e.notes, createdDate = e.createdDate });How do I map the navigation properties EmployeeStatu and ValidEmployeeStatu to my View Models? And how do you then return the values to your models from the View Model objects for SaveChanges()? Thank you.
最满意答案
您应该在第一级模型中执行相同操作。 所以在你的EmployeeViewModel内部创建就像:
employeeStatus = e.EmployeeStatus.Select(s=>new EmployeeStatusViewModel{...}.ToList()当回到模型相同类型的映射但从ViewModel到Model时。 或者您可以签出自动化程序。 搜索博客。
You should do the same as in first level of models. So inside yours creation of EmployeeViewModel do sth like:
employeeStatus = e.EmployeeStatus.Select(s=>new EmployeeStatusViewModel{...}.ToList()When moving back to model the same kind of mapping but from ViewModel to Model. Or you can checkout the automapper. Search for blogs.
更多推荐
发布评论