DataAnnoations MetadataTypeAttribute的ComplexType约定问题 [英] ComplexType convention problem with DataAnnoations MetadataTypeAttribute

查看:54
本文介绍了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 ;
}
}

解决方案

RenegadeXX,


 


这看起来像是CTP5中的一个错误。 
解决方法是明确告诉Code First Address类是一个复杂类型:


 


   
[ MetadataType typeof AddressMetadata ))]


   
[ ComplexType ]


   
public class
地址


   
{


 


您也可以使用流畅的API来执行此操作:


 


   
受保护 覆盖
void OnModelCreating( ModelBuilder modelBuilder)


   
{


       
modelBuilder.ComplexType< 地址>();


   
}


 


谢谢,


亚瑟


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;
  }
 }

解决方案

RenegadeXX,

 

This looks like a bug in CTP5.  The workaround is to explicitly tell Code First that the Address class is a complex type:

 

    [MetadataType(typeof(AddressMetadata))]

    [ComplexType]

    public class Address

    {

 

You could also use the fluent API to do this:

 

    protected override void OnModelCreating(ModelBuilder modelBuilder)

    {

        modelBuilder.ComplexType<Address>();

    }

 

Thanks,

Arthur


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

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