我怎么能覆盖部分类的属性? [英] How can I override the properties in partial class?

查看:124
本文介绍了我怎么能覆盖部分类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个MVC应用程序,我已经使用EF 4.0在开发它。我已经从模型创建的类。现在,我想添加更多的类通过MVC做每一个类。

恩。在下面code,我得到的类位置。 现在,我想创建一个多个类(局部类)我怎样才能在局部类中重写属性?

如何做到这一点?

 命名空间实体
{
   公共部分类位置
   {
       公众诠释标识{搞定;组; }       公共字符串名称{;组; }
       公共字符串备注{搞定;组; }
       公共字符串状态{搞定;组; }
       公共字符串区域{搞定;组; }
       公共字符串引脚code {搞定;组; }       公共虚拟的ICollection<&评论GT;评论{搞定;组; }
   }
}


解决方案

您可以做装饰的属性在部分类与接口

如果您已经生成了下面的类(通过任何自定义工具)

 公共部分类位置
{
    公众诠释标识{搞定;组; }
    公共字符串名称{;组; }
    公共字符串备注{搞定;组; }
    公共字符串状态{搞定;组; }
    公共字符串区域{搞定;组; }
    公共字符串引脚code {搞定;组; }
    公共虚拟的ICollection<&评论GT;评论{搞定;组; }
}

您可以添加注释在生成的类属性(无需修改生成的文件),通过创建一个新的界面和新的部分类,如下

 公共接口ILocation
    {
        [StringLength(50的ErrorMessage =区可以接受最多50个字符。)
        串区域{搞定;组; }
    }    公共部分类地点:ILocation
    {
    }

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.

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 ?

How to do that ?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆