簡體   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