本文介绍了DataAnnoations MetadataTypeAttribute的ComplexType约定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩ctp5。对于ComplexType约定,我有以下szenario(不包括App.config):

 
public class TestContext:DbContext
{
public DbSet< Customer>客户
{
get ;
set ;
}
}

public class CustomerMetadata
{
[StringLength(150)]
[必需]
public 对象名称
{
get ;
set ;
}
}

public class AddressMetadata
{

[StringLength(100,ErrorMessage = ""街道名称太长。")]
[必需(AllowEmptyStrings = false ,ErrorMessage = "街道名称是必需的。" )]
public object Street
{
get ;
set ;
}

[StringLength(150,ErrorMessage = ""城市名称太长。")]
[必需(AllowEmptyStrings = false ,ErrorMessage = ""城市名称是必需的。")]
public object City
{
get ;
set ;
}

[StringLength(5,ErrorMessage = ""邮政编码太长。")]
[必需(AllowEmptyStrings = false ,ErrorMessage = ""邮政编码是必需的。")]
[RegularExpression(" ^ [0-9] {4,5} $" ,ErrorMessage = "邮政编码无效。")]
public object Postalcode
{
get ;
set ;
}
}


[MetadataType( typeof (CustomerMetadata))]
public class 客户
{
公开 int Id
{
get ;
内部 set ;
}
public string 名称
{
get ;
set ;
}
public 地址OfficeAddress
{
get ;
内部 set ;
}
}

[MetadataType( typeof (AddressMetadata))]
public class 地址
{
public string Street
{
get ;
set ;
}
public string Postalcode
{
get ;
set ;
}
public string 城市
{
get ;
set ;
}
}
解决方案

I'm playing a little bit with the ctp5. For the ComplexType-convention I have the following szenario (App.config not included):

 public class TestContext : DbContext
 {
  public DbSet<Customer> Customers
  {
   get;
   set;
  }
 }

 public class CustomerMetadata
 {
  [StringLength(150)]
  [Required]
  public object Name
  {
   get;
   set;
  }
 }

 public class AddressMetadata
 {

  [StringLength(100, ErrorMessage = "The street name is too long.")]
  [Required(AllowEmptyStrings = false, ErrorMessage = "The street name is required.")]
  public object Street
  {
   get;
   set;
  }

  [StringLength(150, ErrorMessage = "The city name is too long.")]
  [Required(AllowEmptyStrings = false, ErrorMessage = "The city name is required.")]
  public object City
  {
   get;
   set;
  }

  [StringLength(5, ErrorMessage = "The postal code is too long.")]
  [Required(AllowEmptyStrings = false, ErrorMessage = "The postal code is required.")]
  [RegularExpression("^[0-9]{4,5}$", ErrorMessage = "The postal code is not valid.")]
  public object Postalcode
  {
   get;
   set;
  }
 }


 [MetadataType(typeof(CustomerMetadata))]
 public class Customer
 {
  public int Id
  {
   get;
   internal set;
  }
  public string Name
  {
   get;
   set;
  }
  public Address OfficeAddress
  {
   get;
   internal set;
  }
 }

 [MetadataType(typeof(AddressMetadata))]
 public class Address
 {
  public string Street
  {
   get;
   set;
  }
  public string Postalcode
  {
   get;
   set;
  }
  public string City
  {
   get;
   set;
  }
 }
解决方案


这篇关于DataAnnoations MetadataTypeAttribute的ComplexType约定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:56