2017-06-21 116 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似乎并不具有截至目前任何影响。