简体   繁体   English

如何在mvc DB中使用模型第一种模式?

[英]How to use model in mvc DB First pattern?

this class comes from db 这个类来自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 我想在mvc中使用类CompanyModel作为模型,但它不起作用

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. 然后在您的控制器操作中,您可以在域EF模型和将传递给视图的视图模型之间进行映射。 To simplify this mapping you could use a tool like AutoMapper . 要简化此映射,您可以使用AutoMapper类的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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