2017-06-21 98 views
2

因此,我目前正在嘗試使用實體框架核心爲表格顯示應用程序用戶已完成哪些演講,並首次創建代碼。我的模型是這樣的:實體框架核心「實體類型'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作爲外鍵/主鍵?

這裏是我的Lecture型號:

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

您無法單獨使用數據註釋來定義compoiste鍵。您需要使用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類需要正確地定義了主鍵。 [Key]是明確告訴EntityFramework將其設置爲主鍵的主鍵註釋,否則約定將接管。

也就是說,名稱爲ID或後綴爲ID(例如, PokemonIDPokemon表。在這種情況下,IDId不區分大小寫。

外鍵屬性僅用於您希望LectureCompletion類中的外鍵屬性名稱與您的引用類名稱不同的情況。例如,如果您的ApplicationUser類的主鍵是ApplicationUserId,但在LectureCompletion類中您希望它是UserId,那麼您可以添加該屬性。

做這樣的

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

至於核心的EntityFramework的ColumnOrder似乎並不具有截至目前任何影響。