如何覆盖分部类中的属性?

nil*_*oru 0 model-view-controller asp.net-mvc entity entity-framework-4

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

恩.在下面的代码中,我得到了类Location.现在,我想再创建一个类(Partial class)如何覆盖分部类中的属性?

怎么做 ?

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; }
   }    
}
Run Code Online (Sandbox Code Playgroud)

Not*_*ple 12

您可以使用接口在部分类中进行属性修饰

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

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; }
}
Run Code Online (Sandbox Code Playgroud)

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

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

    public partial class Location :ILocation
    {
    }
Run Code Online (Sandbox Code Playgroud)