不久前,我问了这个问题:What is the purpose of each of the System.ComponentModel.DataAnnotations attributes?
然而,我没有得到回复。这个问题有点宽泛,因为它要求提供有关每个数据注释属性的文档。此时,我最感兴趣的是Association属性。
我正在使用ASP.NET MVC3和Entity Framework4,并希望对我的POCOs进行注释。我在我的POCOs中使用了外键(不知何故感觉不对,但似乎被普遍接受了)。我如何用Association属性注释我的POCO?我应该把它放在哪些属性上(Association属性和/或外键属性)?什么是thisKey和otherKey参数。thisKey是这个POCOs键,还是这个POCO中的外键?
最后,什么将使用此属性?在ASP.NET MVC中有什么东西吗?
提前感谢!
发布于 2011-03-22 18:47:43
AssociationAttribute (与ExternalReferenceAttribute结合使用)由Silverlight中的WCF RIA服务使用。通过此属性,客户端可以解析来自不同域的实体之间的关联。
我认为它是特定于RIA服务的。Here是如何在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;}
}发布于 2011-03-22 18:22:48
请注意,并不是DataAnnotations命名空间中提供的所有属性都与实体框架相关。我以为在Linq-to-sql中使用的是AssociationAttribute,但实际上它是与System.Data.Linq程序集同名的不同类。我刚刚通过Reflector检查了AssociationAttribute的使用情况,看起来实体框架(包括代码优先)、ASP.NET MVC、ASP.NET Dynamic data或WPF都没有使用它。
https://stackoverflow.com/questions/5389603
复制相似问题