如何覆盖部分类中的属性? [英] How can I override the properties in partial class?
问题描述
我正在开发一个 MVC 应用程序,我在开发它时使用了 EF 4.0.我已经从模型创建了类.现在,我想为 MVC 制作的每个类添加更多类.
I am developing a MVC application, I have used EF 4.0 while developing it. I have created the classes from the model. Now, I want to add more class for each class made by MVC.
例如.在下面的代码中,我得到了类 Location.现在,我想再创建一个类(部分类)如何覆盖部分类中的属性?
ex. In below code, I get the class Location. Now, I want to create one more class(Partial class) How can I override properties in 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; }
}
}
推荐答案
可以在带有接口的部分类中进行属性修饰
You can do attribute decoration in a partial class with an interface
如果您生成了以下类(通过任何自定义工具)
If you have generated the following class (via whatever custom tool)
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; }
}
您可以通过创建一个新的接口和一个新的分部类来为生成的类中的属性添加注释(无需修改生成的文件)
You can add annotations to properties in the generated class (without modifying the generated file) by creating a new interface and a new partial class as below
public interface ILocation
{
[StringLength(50, ErrorMessage = "Region can accept maximum 50 characters.")]
string Region { get; set; }
}
public partial class Location :ILocation
{
}
这篇关于如何覆盖部分类中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!