我正在开发一个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; }
}
}
答案 0 :(得分: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; }
}
您可以通过创建新接口和新的分部类来为生成的类中的属性添加注释(无需修改生成的文件)
public interface ILocation
{
[StringLength(50, ErrorMessage = "Region can accept maximum 50 characters.")]
string Region { get; set; }
}
public partial class Location :ILocation
{
}
答案 1 :(得分:0)
如果您只需要验证,则可以使用所谓的元数据类型。
详细教程为here。