繁体   English   中英

从实体框架 ASP.NET MVC 生成的 Model 创建 ViewModel

[英]Create ViewModel from Model generated by Entity Framework ASP.NET MVC

再会,

我有一个 ASP.NET MVC 项目,我正在使用实体框架和从数据库中自动生成的模型。 我想向字段添加一些属性,但想将它们添加到 ViewModels 而不是 Models,因为 EF 会在刷新时覆盖这些属性。 请告知此 ViewModel 的正确语法,因为我收到错误“无效的 Model 配置”。 我想从 WorkOrder Model 生成 WorkOrderPartNo ViewModel

当前自动生成的工单 model:

namespace EMS.Models
{
using System;
using System.Collections.Generic;

public partial class WorkOrder
{
    public WorkOrder()
    {
        this.WorkOrder1 = new HashSet<WorkOrder>();
    }

    public long ID { get; set; }
    public string WONumber { get; set; }
    public string ExtWONo { get; set; }
    public string WO_Description { get; set; }
    public Nullable<int> HierarchyObjectID { get; set; }
    public Nullable<int> ProductiveUnitID { get; set; }
    public Nullable<int> ComponentID { get; set; }
    public Nullable<int> ComponentCodeID { get; set; }
    public Nullable<int> PositionCodeID { get; set; }
    public int ActivityTypeID { get; set; }
    public int PackageID { get; set; }
    public Nullable<long> ParentWO { get; set; }
    public string Type { get; set; }
    public string Priority { get; set; }
    public Nullable<System.DateTime> BasicStart { get; set; }
    public Nullable<System.DateTime> BasicFin { get; set; }
    public Nullable<System.DateTime> SchedStart { get; set; }
    public Nullable<System.DateTime> SchedFin { get; set; }
    public System.DateTime RefDate { get; set; }
    public Nullable<int> WorkGroupID { get; set; }
    public Nullable<float> DurationHrs { get; set; }
    public Nullable<float> ActWork { get; set; }
    public string WOStatusID { get; set; }
    public Nullable<int> ForecastItemID { get; set; }
    public string IssueNo { get; set; }
    public Nullable<int> PartID { get; set; }
    public Nullable<double> Quantity { get; set; }
    public Nullable<int> IssueUOM { get; set; }
    public Nullable<decimal> ActTotSum { get; set; }
    public Nullable<decimal> SumTotPlan { get; set; }
    public string CurrencyID { get; set; }
    public Nullable<int> AccountID { get; set; }
    public bool Exclude { get; set; }
    public bool Reviewed { get; set; }
    public Nullable<System.DateTime> CreatedOn { get; set; }
    public Nullable<System.DateTime> LastChangedOn { get; set; }
    public string LastChangedBy { get; set; }
    public bool Exported { get; set; }

    public virtual ActivityType ActivityType { get; set; }
    public virtual Package Package { get; set; }
    public virtual PartMaster PartMaster { get; set; }
    public virtual PositionCode PositionCode { get; set; }
    public virtual WO_CalcUOM WO_CalcUOM { get; set; }
    public virtual WOStatu WOStatu { get; set; }
    public virtual ICollection<WorkOrder> WorkOrder1 { get; set; }
    public virtual WorkOrder WorkOrder2 { get; set; }
    public virtual Component Component { get; set; }
    public virtual ComponentCode ComponentCode { get; set; }
    public virtual HierarchyObject HierarchyObject { get; set; }
    public virtual ForecastItem ForecastItem { get; set; }
    public virtual Account Account { get; set; }
    public virtual UOM_Procurement UOM_Procurement { get; set; }
    public virtual ProductiveUnit ProductiveUnit { get; set; }
    public virtual WorkGroup WorkGroup { get; set; }
   }
}

给出错误的 WorkOrderPartNo ViewModel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EMS.Models;


namespace EMS.ViewModels
{
using System;
using System.Collections.Generic;

public partial class WorkOrderPartNo : WorkOrder
{
    public WorkOrderPartNo()
    {
        this.WorkOrder1 = new HashSet<WorkOrder>();
    }

    public new long ID { get; set; }
    public new string WONumber { get; set; }
    public new string ExtWONo { get; set; }
    public new string WO_Description { get; set; }
    public new Nullable<int> HierarchyObjectID { get; set; }
    public new Nullable<int> ProductiveUnitID { get; set; }
    public new Nullable<int> ComponentID { get; set; }
    public new Nullable<int> ComponentCodeID { get; set; }
    public new Nullable<int> PositionCodeID { get; set; }
    public new int ActivityTypeID { get; set; }
    public new int PackageID { get; set; }
    public new Nullable<long> ParentWO { get; set; }
    public new string Type { get; set; }
    public new string Priority { get; set; }
    public new Nullable<System.DateTime> BasicStart { get; set; }
    public new Nullable<System.DateTime> BasicFin { get; set; }
    public new Nullable<System.DateTime> SchedStart { get; set; }
    public new Nullable<System.DateTime> SchedFin { get; set; }
    public new System.DateTime RefDate { get; set; }
    public new Nullable<int> WorkGroupID { get; set; }
    public new Nullable<float> DurationHrs { get; set; }
    public new Nullable<float> ActWork { get; set; }
    public new string WOStatusID { get; set; }
    public new Nullable<int> ForecastItemID { get; set; }
    public new string IssueNo { get; set; }
    [UIHint("ComboBox")]
    public new Nullable<int> PartID { get; set; }
    public new Nullable<double> Quantity { get; set; }
    public new Nullable<int> IssueUOM { get; set; }
    public new Nullable<decimal> ActTotSum { get; set; }
    public new Nullable<decimal> SumTotPlan { get; set; }
    public new string CurrencyID { get; set; }
    public new Nullable<int> AccountID { get; set; }
    public new bool Exclude { get; set; }
    public new bool Reviewed { get; set; }
    public new Nullable<System.DateTime> CreatedOn { get; set; }
    public new Nullable<System.DateTime> LastChangedOn { get; set; }
    public new string LastChangedBy { get; set; }
    public new bool Exported { get; set; }

 //   public new virtual ActivityType ActivityType { get; set; }
  //  public new virtual Package Package { get; set; }
 //   public new virtual PartMaster PartMaster { get; set; }
 //   public new virtual PositionCode PositionCode { get; set; }
 //   public new virtual WO_CalcUOM WO_CalcUOM { get; set; }
 //   public new virtual WOStatu WOStatu { get; set; }
 //   public new virtual ICollection<WorkOrder> WorkOrder1 { get; set; }
 //   public new virtual WorkOrder WorkOrder2 { get; set; }
 //   public new virtual Component Component { get; set; }
 //   public new virtual ComponentCode ComponentCode { get; set; }
 //   public new virtual HierarchyObject HierarchyObject { get; set; }
 //   public new virtual ForecastItem ForecastItem { get; set; }
 //   public new virtual Account Account { get; set; }
 //   public new virtual UOM_Procurement UOM_Procurement { get; set; }
 //   public new virtual ProductiveUnit ProductiveUnit { get; set; }
 //   public new virtual WorkGroup WorkGroup { get; set; }
    }
}

帮助表示赞赏

继承 class 然后将此 class 的所有属性标记为新属性对我来说没有任何意义。 它与没有任何 inheritance 的新 class 相同。 并且你使用 this.WorkOrder1 = new HashSet() 两次,你不需要再次重复它,因为你已经在基础 class 中访问它。

我将在新文件 WorkOrderPartNo.cs 中创建部分 WorkOrder class 并添加一些您需要的额外属性。 不要担心虚拟属性,因为它是延迟加载。 这样,它不会覆盖您的主要 WorkOrder.cs,而是拥有所有 WorkOrderProperties:

//WorkOrderPartNo.cs file

public partial class WorkOrder
{
[NotMapped]
public property1 {get; set;}
......
.......
    
}

正如@Sergey 所说,从您的实体继承不是一个好主意。 如果我站在你的立场上,我会使用映射器来创建 DTO,而不是使用 NotMapped 属性或 inheritance 方法。

Automapper 和 Mapster 是我最喜欢的映射器。

仅供参考,当您使用映射器时,您可以通过投影增强查询,这对于提高查询性能非常有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM