我在我的模型(由 EF 生成)字段周围添加了一些属性(例如:[DisplayName("Office Contact")])当我从数据库更新我的模型时,我的模型类被重新生成,我显然丢失了这些属性。
有解决方法吗?
谢谢。
以下是我在 EF 模型上使用属性的原因:
EF 模型:Office.cs 我的 ViewModel:NewOfficeViewModel.cs
但 Office 是我的 viewModel 的属性;所以我不得不把属性放在办公室属性周围。
这被认为是错误的做法吗?我应该在我的视图模型类中重复我需要的 EF 模型类的属性并手动映射它们吗?对我来说听起来重复...
谢谢您的帮助。
这是我的代码:
视图模型:
公共类 NewOfficeViewModel { 私有 UnitOfWork unitOfWork = new UnitOfWork();
public NewOfficeViewModel()
{
var vendors = unitOfWork.VendorRepository.Get();
var languages = unitOfWork.LanguageRepository.Get();
var correspondences = unitOfWork.PreferredCorrespondenceRepository.Get();
var provinces = unitOfWork.ProvinceRepository.Get();
var culture = CultureHelper.GetCurrentCulture();
Vendors = new SelectList(vendors, "VendorID", "Product");
Languages = new SelectList(languages, "LangID", culture == "fr" ? "Text_fr" : "Text_en");
Correspondences = new SelectList(correspondences, "PreferredCorrespondenceID", "PreferredCorrespondence1");
Provinces = new SelectList(provinces, "ProvinceID", "Province1");
}
public Office Office { get; set; }
public SelectList Provinces { get; set; }
public SelectList Vendors { get; set; }
public SelectList Languages { get; set; }
public SelectList Correspondences { get; set; }
}
英孚型号:
公共部分类 Office { public Office() { this.ContactOffices = new HashSet(); }
public int OfficeID { get; set; }
public Nullable<int> OfficePredefinedNumberID { get; set; }
public int OfficeTypeID { get; set; }
[LocalizedDisplayName("CompanyName")]
public string CompanyName { get; set; }
[LocalizedDisplayName("OfficeContact")]
public string OfficeContact { get; set; }
[LocalizedDisplayName("Address1")]
public string Address1 { get; set; }
[LocalizedDisplayName("Address2")]
public string Address2 { get; set; }
[LocalizedDisplayName("City")]
public string City { get; set; }
[LocalizedDisplayName("PostalCode")]
public string PostalCode { get; set; }
[LocalizedDisplayName("Phone")]
public string Phone { get; set; }
[LocalizedDisplayName("Fax")]
public string Fax { get; set; }
[LocalizedDisplayName("Email")]
public string Email { get; set; }
[LocalizedDisplayName("Language")]
public int LangID { get; set; }
[LocalizedDisplayName("PreferredCorrespondence")]
public int PreferredCorrespondenceID { get; set; }
[LocalizedDisplayName("Software")]
public int VendorID { get; set; }
public Nullable<System.DateTime> ProfileSentDate { get; set; }
public string OfficeNote { get; set; }
public int CreatedByContactID { get; set; }
public System.DateTime CreatedDate { get; set; }
public int ModifiedByContactID { get; set; }
public System.DateTime ModifiedDate { get; set; }
public int ChangeLogID { get; set; }
[DisplayName("Province")]
public int ProvinceID { get; set; }
public int StatusID { get; set; }
public virtual ICollection<ContactOffice> ContactOffices { get; set; }
public virtual Vendor Vendor { get; set; }
public virtual Province Province { get; set; }
public virtual OfficePredefinedNumber OfficePredefinedNumber { get; set; }
public virtual Status Status { get; set; }
}