c# - C# 对象中 [key] 的用途是什么?

我第一次尝试用 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 可以修饰类、方法、属性或程序集。它们自己不做任何事情,而是被其他代码用来“标记”事物。

https://stackoverflow.com/questions/24287993/

相关文章:

spring - 错误 : "Connection refused: connect. 验证连接属性

ports - 代理服务器的推荐端口

sorting - thenComparing 与 thenComparingInt 的性能 - 使

git - 从 git 中的分支中提取最新提交

r - 按第一列对矩阵列表进行排序

oracle - Mysql Workbench 将 sql 导出到 Oracle 11

hibernate - 在 JPA 中同时映射键和对象

design-patterns - 策略接口(interface)中的策略模式和方法数量

ruby-on-rails - 不知道如何构建任务 - cucumber

json - 在 JSON-RPC 连接上读取多个 JSON 对象