0

I added some attributes around my model (generated by EF) fields (ex: [DisplayName("Office Contact")]) When i update my model from the database my model class gets regenerated and i obviously i lose those attributes.

is there a workaround around this?

Thanks.

HERE IS THE REASON WHY I AM USING ATTRIBUTES ON THE EF MODEL:

EF model: Office.cs my ViewModel: NewOfficeViewModel.cs

but Office is a property of my viewModel; so i had to put the attributes around the office properties instead.

Is this considered wrong practice? should i repeat the properties that i need from the EF model class in my view model class and map them manually? sounds repetitive to me...

Thanks for the help.

Here is my code:

ViewModel:

public class NewOfficeViewModel { private 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; }


}

EF Model:

public partial class 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; }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
  • Is there a reason you are decorating EF fields with displayname etc? Usually the display name would be the value that goes on your view model, not the model. Are you using these as view objects? I'd strongly recommend mapping these to a viewmodel (or model in MVC) and using that instead - you shouldn't use entity classes as model objects – Charleh Nov 08 '13 at 14:48
  • 1
    You need to build `partial` classes for your entities and attribute the properties in those. – Mike Perrenoud Nov 08 '13 at 14:48
  • Wouldn't that require partial properties? – Charleh Nov 08 '13 at 14:49
  • 1
    @Charleh, no you can actually markup the meta-data with `partial` classes. There is a specific definition for those classes and I can't remember that off hand right now. – Mike Perrenoud Nov 08 '13 at 14:49
  • You mean use `MetadataType` attribute? I've seen it, don't know if I like it much but it shouldwork in this case – Charleh Nov 08 '13 at 14:52
  • That approach will work in this, but I do agree with your original comment, Charleh. I answered a question about why putting validation on entities is such a problem yesterday, if you guys are interested: http://stackoverflow.com/questions/19838772/how-can-i-ignore-missing-fields-from-the-metadatatype-attribute/19838920#19838920 – John H Nov 08 '13 at 14:58
  • @neoistheone, is it possible to have `public string Name{get;set;}` in one partial file and another one partial file with same property decorated with `MetaTag` like `Display, Required`? – Murali Murugesan Nov 08 '13 at 15:04
  • @Murali, yes that should work just fine; even if `Name` were an extended property because the partial classes have the same `namespace` and type name. – Mike Perrenoud Nov 08 '13 at 15:13

1 Answers1

1

you shouldn't be using your entity framework objects/classes with the view. you should be creating a view model (basically mapping the properties from your entity to the model/view model) and binding it to the view. you also avoid problems like this. you are somewhat defeating the purpose of MVC - the model part of the architecture

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
  • i agree with you about using a viewModel. and this is what i have. please check again my question i edited it, stating why i'm using the EF class for my data annotation properties. Thanks for the help – user2964813 Nov 08 '13 at 15:15
  • remember, the problem is the designer overwrites your changes. that is expected when you regenerate the model. it is not supposed to reserve your changes and you also shouldn't be adding things like this - unless you of course back it up, regen and then put the changes back in place. you need to have a view/viewmodel which maps to and from the entity – Ahmed ilyas Nov 08 '13 at 18:39