0

私はMVCアプリケーションを開発していますが、開発中にEF4.0を使用しました。モデルからクラスを作成しました。ここで、MVCで作成されたクラスごとにクラスを追加したいと思います。

元。以下のコードでは、クラスLocationを取得します。ここで、もう1つのクラス(部分クラス)を作成したいのですが、部分クラスのプロパティをオーバーライドするにはどうすればよいですか?

どうやってするか ?

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

If all you need is validation, you can use so-called metadata types.

Detailed tutorial is here.

于 2012-08-04T10:21:59.640 に答える