将实体框架导航属性映射到ViewModel对象(Map Entity Framework navigation properties to ViewModel objects)

编程入门 行业动态 更新时间:2024-10-23 17:32:22
实体框架导航属性映射到ViewModel对象(Map Entity Framework navigation properties to ViewModel objects)

我正在使用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.

更多推荐

本文发布于:2023-08-04 15:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   框架   属性   对象   ViewModel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!