是否可以在同一个 class 中设置两个键?

Is it possible to have two keys in the same class?

我有一个问题。我真的不知道这是否可能。我正在处理 MVC 应用程序,部分模型如下所示:

public class EmployeeNewspaper
{
    public string Name { get; set; }
    [Key]
    public string Appendage { get; set; }
    public string SourceInformationName { get; set; }
}

所以我的问题是:是否可以在同一个 class 中设置两个键?或者我能做什么?我需要有唯一的名称和附件。可能吗?

您可以使用 Guid 并获得 Globally Unique ID实体。

但是您需要注意使用这个:

Guid MyID = new Guid();

将导致全零(空)ID。

所以你需要这样使用它:

Guid MyID = Guid.NewGuid();

谢谢 @Charle我实际上是在寻找复合键,答案是:

public class EmployeeNewspaper
{
    [Key][Column(Order = 0)]
    public string Name { get; set; }
    [Key][Column(Order = 1)]
    public string Appendage { get; set; }
    public string SourceInformationName { get; set; }
}