Asp.net mvc MVC模型属性被覆盖

Asp.net mvc MVC模型属性被覆盖,asp.net-mvc,entity-framework,Asp.net Mvc,Entity Framework,我在模型(由EF生成)字段周围添加了一些属性(例如:[DisplayName(“Office联系人”)])) 当我从数据库更新我的模型时,我的模型类被重新生成,我显然丢失了这些属性 有解决办法吗 谢谢 以下是我在EF模型上使用属性的原因: EF模型:Office.cs 我的ViewModel:NewOfficeViewModel.cs 但办公室是我的viewModel的财产;因此,我不得不将属性放在office属性周围 这被认为是错误的做法吗?我是否应该在视图模型类中重复EF模型类所需的属性并手

我在模型(由EF生成)字段周围添加了一些属性(例如:[DisplayName(“Office联系人”)])) 当我从数据库更新我的模型时,我的模型类被重新生成,我显然丢失了这些属性

有解决办法吗

谢谢

以下是我在EF模型上使用属性的原因:

EF模型:Office.cs 我的ViewModel:NewOfficeViewModel.cs

但办公室是我的viewModel的财产;因此,我不得不将属性放在office属性周围

这被认为是错误的做法吗?我是否应该在视图模型类中重复EF模型类所需的属性并手动映射它们?听起来很重复

谢谢你的帮助

这是我的密码:

视图模型:

公共类NewOfficeViewModel { 私有UnitOfWork UnitOfWork=新UnitOfWork()

EF模型:

公共部分班级办公室 { 公职() { 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; }
}
public int OfficeID{get;set;}
公共可为空的OfficePreferencedNumberId{get;set;}
public int OfficeTypeID{get;set;}
[本地化显示名称(“公司名称”)]
公共字符串CompanyName{get;set;}
[本地化显示名称(“OfficeContact”)]
公共字符串OfficeContact{get;set;}
[本地化显示名称(“地址1”)]
公共字符串地址1{get;set;}
[本地化显示名称(“地址2”)]
公共字符串地址2{get;set;}
[本地化显示名称(“城市”)]
公共字符串City{get;set;}
[本地化显示名称(“PostalCode”)]
公共字符串PostalCode{get;set;}
[本地化显示名称(“电话”)]
公用字符串电话{get;set;}
[本地化显示名称(“传真”)]
公共字符串传真{get;set;}
[本地化显示名称(“电子邮件”)]
公共字符串电子邮件{get;set;}
[本地化显示名称(“语言”)]
public int LangID{get;set;}
[本地化显示名称(“首选通信”)]
public int preferredRespondenceId{get;set;}
[本地化显示名称(“软件”)]
public int VendorID{get;set;}
公共可为空的ProfileSentDate{get;set;}
公共字符串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;}
[显示名称(“省”)]
public int ProvinceID{get;set;}
public int StatusID{get;set;}
公共虚拟ICollection联系人办公室{get;set;}
公共虚拟供应商{get;set;}
公共虚拟省省{get;set;}
公共虚拟OfficePreferencedNumber OfficePreferencedNumber{get;set;}
公共虚拟状态状态{get;set;}
}

您不应该在视图中使用实体框架对象/类。您应该创建视图模型(基本上是将属性从实体映射到模型/视图模型)并将其绑定到视图。您还可以避免类似的问题。
您有点违背了MVC的目的-架构的模型部分

是否有理由使用displayname等装饰EF字段?通常显示名称是视图模型上的值,而不是模型。您是否将这些用作视图对象?我强烈建议将其映射到viewmodel(或MVC中的模型)相反,使用它-你不应该使用实体类作为模型对象你需要为你的实体构建
partial
类并对其中的属性进行属性化。这不需要部分属性吗?@Charleh,不,你实际上可以用
partial
类标记元数据。这些类有一个特定的定义sses和我现在都记不起来了。你是说使用
MetadataType
属性?我看过,不知道我是否很喜欢它,但在这种情况下它应该可以工作。我同意你关于使用viewModel的看法。这就是我所拥有的。请再次检查我编辑它时提出的问题,说明为什么我在数据注释属性中使用EF类ies。感谢您的帮助请记住,问题是设计器会覆盖您的更改。这在您重新生成模型时是应该的。它不应该保留您的更改,您也不应该添加类似的内容-当然,除非您备份它,重新生成,然后将更改放回原位。您需要一个映射到实体和从实体映射
    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; }
}