我如何使用System.ComponentModel.DataAnnotations.AssociationAttribute

Mat*_*els 9 poco entity-framework-4 data-annotations asp.net-mvc-3

前段时间我问过这个问题:每个System.ComponentModel.DataAnnotations属性的目的是什么?

但是,我没有收到回复的运气.这个问题有点宽泛,因为它要求提供有关每个dataannotation属性的文档.此刻,我最感兴趣的是Association属性.

我正在使用ASP.NET MVC3和Entity Framework 4,并希望注释我的POCO.我在我的POCO中使用外键(不知何故感觉不对,但似乎通常是加入的).如何使用Association属性注释我的POCO ?我把它放在哪些属性(Association属性和/或外键属性)?有什么thisKeyotherKey参数.thisKey这个POCO 是关键还是这个POCO中的外键?

最后,什么会使用这个属性?ASP.NET MVC中有什么东西吗?

提前致谢!

Lad*_*nka 6

请注意,并非DataAnnotations命名空间中提供的所有属性都与实体框架相关.我认为它AssociationAttribute在Linq-to-sql中使用,但它实际上是System.Data.Linq程序集中具有相同名称的不同类.我刚刚检查AssociationAttribute了Reflector的用法,它看起来既不是Entity Framework(包括代码优先),ASP.NET MVC,ASP.NET动态数据或WPF都使用它.


Jeh*_*hof 5

AssociationAttribute(与ExternalReferenceAttribute结合使用)由Silverlight中的WCF RIA服务使用.使用此属性,客户端可以解析来自不同域的实体之间的关联.

我认为这是RIA服务特有的.以下是如何在WCF RIA服务中应用它的演练.

一个简单的例子看起来像

public class Customer{
   public int Id {get;set;}
}

public class Order{

  public int Id {get;set;}

  public int CustomerId {get;set;}  //ForeignKey of the Customer

  [ExternalReference]
  [Association("Customer", "CustomerId", "Id")]
  public Customer {get;set;}
}
Run Code Online (Sandbox Code Playgroud)