Fluent Api 列顺序语法

Fluent Api Column order sytax

我正在尝试了解如何将这种多对多注释变成流利的 api。我只是不知道表示列顺序的语法。

public class UserNotification
   {
     [key]
     [Column(Order = 1)]
     public string UserId { get; set;}

     [key]
     [Column(Order = 2)]
     public int NotificationId {get; set;}

     public ApplicationUser User{get; set;}
     public Notification Notification {get; set;}
   }

我知道流利的Api会是这样的:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
   modelBuilder.Entity<UserNotification>()
     .HasKey(n => new {n.UserId, n.NotificationId});

    // What about the Column Order? 
}

您可以阅读KeyColumn数据注释如下:

UserNotification has a key consisting of UserId and NotificationId columns, with UserId being first and NotificationId being second.

即列顺序属性仅用于确定在复合主键的上下文中哪一列是第一列、第二列等。

Fluent API 不需要,因为您在 HasKey 表达式中描述了键列及其顺序:

modelBuilder.Entity<UserNotification>()
   .HasKey(n => new { n.UserId, n.NotificationId });
//                        ^            ^
//                      first        second

换句话说,你做对了,不需要进一步的操作。