简体   繁体   中英

How to use model in mvc DB First pattern?

this class comes from db

public class Company
{
    public new Guid ID { get; set; }
    public new String Name { get; set; }
}

I want to use class CompanyModel as model in mvc but it doesn't works

public class CompanyModel : Company
{
    public new Guid ID { get { return base.ID; } set { base.ID = ID; } }

    [Required]
    [Display(Name = "Company Name")]
    public new String Name { get { return base.Name; } set { base.Name = Name; } }
}

Is there ways to do something like that?

I would recommend you using a view model instead:

public class CompanyViewModel
{
    public new Guid ID { get; set; }

    [Required]
    [Display(Name = "Company Name")]
    public string Name { get; set; }    
}

Then in your controller action you could map between your domain EF model and the view model that will get passed to the view. To simplify this mapping you could use a tool like AutoMapper .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM