2017-06-21 6 views
2

私は現在、アプリケーションユーザーがどの講義を完了したかを表示するテーブルのEntity Framework Coreを使用したコードの最初の移行を作成しようとしています。Entity Frameworkコア "エンティティタイプ 'XXX'は、主キーを定義する必要があります。

public class LectureCompletion 
{ 
    [Key, Column(Order = 0)] 
    [ForeignKey("Lecture")] 
    public Lecture LectureId { get; set; } 

    [Key, Column(Order = 1)] 
    [ForeignKey("User")] 
    public ApplicationUser UserId{ get; set; } 

    public bool Completed { get; set; } 
} 

私はユニークな複合キーとしてUserIdLectureIdを使用したい:私のモデルは、このようになります。しかし、私はこのエラーが発生します:

The entity type 'LectureCompletion' requires a primary key to be defined.

私は明らかに正しい位置に私の重要な属性を持っているので、これがどうして起こっているのか分かりません。外来/主キーとしてApplicationUserを使用できますか?

public class Lecture 
{ 
    [Key] 
    public int LectureId { get; set; } 

    public string ModuleName { get; set; } 
    public string LectureName { get; set; } 
} 

そして、私のApplicationDBContext.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 
    public DbSet<Lecture> Lectures { get; set; } 
    public DbSet<LectureCompletion> LectureCompletion { get; set; } 

    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
     : base(options) 
    { 
    } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 
    } 
} 

答えて

6

データアノテーションだけでコンポジットキーを定義することはできません。代わりにFluent APIを使用する必要があります。

public class LectureCompletion 
{ 
    // which is your case. 
    [ForeignKey("Lecture")] 
    public int LectureId { get;set; } 
    public Lecture Lecture { get; set; } 
    [ForeignKey("ApplicationUser")] 
    public int UserId {get;set;} 
    public ApplicationUser ApplicationUser { get; set; } 
    public bool Completed { get; set; } 
} 


protected override void OnModelCreating(ModelBuilder builder) 
{ 
    base.OnModelCreating(builder); 

    // Define composite key. 
    builder.Entity<LectureCompletion>() 
     .HasKey(lc => new { lc.LectureId, lc.UserId }); 
} 

https://docs.microsoft.com/en-us/ef/core/modeling/keys

+0

すごいです!ありがとうございました!それはあなたが提供したリンクを読んでいくつかのことを学びました。ちょうど、外部キーUserIdがintでなく文字列である必要があることを追加したかったのです。 –

0

あなたLectureCompletionクラスが適切に定義される主キーを必要と

は、ここに私のLectureモデルです。 [Key]は、EntityFrameworkにプライマリキーとして設定するように明示的に指示するプライマリキー注釈です。それ以外の場合はコンベンションが継承されます。

つまり、IDという名前のプロパティまたはIDという名前のプロパティのいずれかです。 Pokemon表のPokemonIDIDまたはIdは、この場合大文字と小文字が区別されません。

外部キー属性は、参照するクラスと異なる名前のLectureCompletionクラスの外部キープロパティ名を必要とする場合にのみ使用されます。たとえば、ApplicationUserクラスの主キーがApplicationUserIdですが、LectureCompletionクラスの場合は、UserIdになります。属性を追加できます。

がEntityFrameworkコアについては、この

public class LectureCompletion 
{ 
    [Key] // Defined only once 
    public LectureCompletionId { get;set; } 

    // Not needed if Lecture class has the primary key property of LectureId, 
    // which is your case. 
    [ForeignKey("Lecture")] // Name of your navigation property below. 
    public int LectureId { get;set; } 

    public Lecture Lecture { get; set; } 

    [ForeignKey("ApplicationUser")] 
    public int UserId {get;set;} 

    public ApplicationUser ApplicationUser { get; set; } 

    public bool Completed { get; set; } 
} 

のようにそれを実行してください、のcolumnOrderは、今のように任意の効果があると表示されません。

関連する問題