实体框架核心"实体类型'XXX'需要定义主键."

Jos*_*h L 11 c# sql entity-framework asp.net-identity asp.net-core

所以我目前正在尝试使用Entity Framework Core创建一个代码,首先用于显示应用程序用户已完成的讲座的表.我的模型看起来像这样:

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; }
}
Run Code Online (Sandbox Code Playgroud)

我想使用UserIdLectureId作为唯一的复合键.但是我收到此错误:

实体类型'LectureCompletion'需要定义主键.

我不明白为什么会这样,因为我明确地将我的关键属性放在正确的位置?我可以ApplicationUser用作外键/主键吗?

这是我的Lecture模型:

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

    public string ModuleName { get; set; }
    public string LectureName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

ans*_*erk 19

您不能单独使用数据注释定义compoiste键.您需要使用Fluent API.

public class LectureCompletion
{
    // which is your case.
    [ForeignKey(nameof(Lecture))] 
    public int LectureId { get;set; }
    public Lecture Lecture { get; set; }
    [ForeignKey(nameof(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 });
}
Run Code Online (Sandbox Code Playgroud)

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


Iva*_*azz 5

您的LectureCompletion类需要一个主键才能正确定义。[Key]是主键注释,显式告诉 EntityFramework 将其设置为主键,否则约定将接管。

也就是说,属性的名称ID或后缀为IDeg PokemonIDfor Pokemontable。ID或者Id在这种情况下不区分大小写。

仅当您希望类中的外键属性名称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; }
}
Run Code Online (Sandbox Code Playgroud)

对于 EntityFramework Core,ColumnOrder 目前似乎没有任何效果。