Reputation: 5
Good day,
I have an ASP.NET MVC project and I'm using Entity Framework with automatically generated models from the database. I would like to add some attributes to fields but would like to add them to ViewModels and not Models as EF will overwrite the attributes upon refresh. Please advise on the correct syntax for this ViewModel as I'm getting the error "Invalid Model Configuration". I would like to generate the WorkOrderPartNo ViewModel from the WorkOrder Model
The current Auto Generated WorkOrder 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; }
}
}
The WorkOrderPartNo ViewModel that gives the error:
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; }
}
}
Help is appreciated
Upvotes: 0
Views: 487
Reputation: 1012
As @Sergey said it's not a good idea to inherit from your entity. If I were in your shoes I would use a mapper to create DTO instead of using the NotMapped property or inheritance approach.
Automapper and Mapster are my favorites mappers.
FYI, when you are using mapper you can enhance your query by a projection which is very useful to improve your query performance.
Upvotes: 0
Reputation: 43959
It doesn't make any sense to me to inherit a class and then to mark all the properties of this class as new. It is the same as a new class without any inheritance. And you use this.WorkOrder1 = new HashSet() twice, you don't need to repeat it again since you have already access to it in the base class.
I would just create a partial WorkOrder class in a new file WorkOrderPartNo.cs and add some extra properties that you need. Don't worry about virtual properties since it is a lazy loading. This way it will not override your main WorkOrder.cs but have all WorkOrderProperties :
//WorkOrderPartNo.cs file
public partial class WorkOrder
{
[NotMapped]
public property1 {get; set;}
......
.......
}
Upvotes: 0