2017-12-15 12 views
1

私はこの多くの注釈を流暢なAPIに入れる方法を理解しようとしています。私は列の順序を表す構文を知らない。流暢なAPI列順序sytax

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

答えて

2

あなたがKeyColumnデータ注釈を読むことができます:

UserNotificationましたキーコンシスティユーザーIDが最初であると通知IDが第二された状態でユーザーID通知ID列のNG、。

すなわち、列の順序属性は、複合主キーの文脈における第一、第二等である列を決定するためにのみ使用されます。

流暢APIを使用すると、HasKey式の中のキー列とその順序の両方を記述するためにすることを必要としません。つまり

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

を、あなたはそれが正しく、それ以上のアクションは必要ありませんでした。

+1

ありがとうございます!それは本当に素晴らしいです。私はすぐに勉強しています。 – AllocSystems

関連する問題