如何使用System.ComponentModel.DataAnnotations.AssociationAttribute [英] How do I use System.ComponentModel.DataAnnotations.AssociationAttribute

查看:165
本文介绍了如何使用System.ComponentModel.DataAnnotations.AssociationAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Some time ago I asked this question: What is the purpose of each of the System.ComponentModel.DataAnnotations attributes?

但是,我没有收到回复的运气。这个问题有点广泛,因为它要求有关每个数据注释属性的文档。在这一刻,我主要关心 Association 属性。

However, I had no luck with getting replies. This question was a bit broad in the sense that it asked for documentation about every dataannotation attribute. At this moment, I am mostly interested in the Association attribute.

我正在使用ASP.NET MVC3与实体框架4,并希望注释我的POCO。我在我的POCO中使用外键(以某种方式感到错误,但是似乎通常是被接受的)。如何使用关联属性注释我的POCO?我在哪些属性(关联属性和/或外键属性)?什么是 thisKey otherKey 参数。这个POCO键或这个POCO中的外键是 thisKey

I am using ASP.NET MVC3 with Entity Framework 4 and would like to annotate my POCOs. I am using foreign keys in my POCOs (somehow feels wrong, but it seems to be generally accepeted). How do I annotate my POCO with the Association attribute? On which properties do I put it (Association property and/or foreign key property)? What are the thisKey and otherKey parameters. Is thisKey this POCOs key, or the foreign key in this POCO?

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

Then lastly, what will use this attribute? Is there something in ASP.NET MVC?

提前感谢!

推荐答案

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

The AssociationAttribute (in conjuction with the ExternalReferenceAttribute) is used by WCF RIA Services in Silverlight. With this attribute the client can resolve associations between entities from diffrent Domains.

我认为这是RIA服务的特定。 这里的演练如何得到适用于WCF RIA服务。

I think it is specific to RIA Services. Here´s a walkthrough how it gets applied in WCF RIA Services.

一个简单的示例看起来像

A simple Example looks like

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

这篇关于如何使用System.ComponentModel.DataAnnotations.AssociationAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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