首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Entity Framework Core“实体类型'XXX‘需要定义主键。”

Entity Framework Core“实体类型'XXX‘需要定义主键。”
EN

Stack Overflow用户
提问于 2017-06-21 10:46:43
回答 2查看 39.4K关注 0票数 20

因此,我目前正在尝试使用Entity Framework Core创建一个代码优先迁移,用于显示应用程序用户完成了哪些讲座。我的模型是这样的:

代码语言:javascript
运行
复制
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作为唯一的组合键。然而,我得到了这个错误:

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

我不明白为什么会发生这种情况,因为我清楚地将关键属性放在了正确的位置?我可以使用ApplicationUser作为外键/主键吗?

这是我的Lecture模型:

代码语言:javascript
运行
复制
public class Lecture
{
    [Key]
    public int LectureId { get; set; }

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

还有我的ApplicationDBContext.cs

代码语言:javascript
运行
复制
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);
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44666042

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档