0

我正在开发一个 MVC 应用程序,我在开发它时使用了 EF 4.0。我已经从模型中创建了类。现在,我想为 MVC 制作的每个类添加更多类。

前任。在下面的代码中,我得到了类位置。现在,我想再创建一个类(部分类)如何覆盖部分类中的属性?

怎么做 ?

namespace Entities
{
   public partial class Location
   {               
       public int Id { get; set; }

       public string Name { get; set; }
       public string Remark { get; set; }      
       public string State { get; set; }       
       public string Region { get; set; }
       public string PinCode { get; set; }

       public virtual ICollection<Comment> Comments { get; set; }
   }    
}
4

2 回答 2

14

您可以在具有接口的部分类中进行属性装饰

如果您生成了以下类(通过任何自定义工具)

public partial class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Remark { get; set; }
    public string State { get; set; }
    public string Region { get; set; }
    public string PinCode { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

您可以通过创建一个新接口和一个新的分部类,如下所示为生成的类中的属性添加注释(无需修改生成的文件)

    public interface ILocation
    {
        [StringLength(50, ErrorMessage = "Region can accept maximum 50 characters.")]
        string Region { get; set; }
    }

    public partial class Location :ILocation
    {
    }
于 2012-08-04T10:04:49.820 回答
0

如果您只需要验证,则可以使用所谓的元数据类型。

详细教程在这里

于 2012-08-04T10:21:59.640 回答