我第一次尝试用 C# 编写 WCF RIA 类(也是我第一次使用该语言),只是为了连接一些表以获得 LightSwitch 数据源。关注example code ,我创建了以下类来返回我的数据行:
public class StudentTerm
{
[Key]
public string TermCode { get; set; }
public string StudentID { get; set; }
public decimal? TermGPA { get; set; }
public decimal? CumulativeGPA { get; set; }
}
但我很好奇——[Key] typedef 的目的是什么?它仅适用于我示例中的 TermCode,还是适用于整个类(class)?
最佳答案
正如@LIUFA 上面所说,Key
是一个属性。它的全称是 System.ComponentModel.DataAnnotations.KeyAttribute
这是为了表示 Id 字段是底层数据存储的主键。
在更一般的意义上,Attribute
可以修饰类、方法、属性或程序集。它们自己不做任何事情,而是被其他代码用来“标记”事物。
关于c# - C# 对象中 [key] 的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24287993/