java 主键类型,实体框架核心“实体类型'XXX'需要定义主键 . ”

所以我现在正在尝试使用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; }

}

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

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

我不明白为什么会这样,因为我明确地将我的关键属性放在正确的位置?我可以使用 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

{

public DbSet Lectures { get; set; }

public DbSet LectureCompletion { get; set; }

public ApplicationDbContext(DbContextOptions 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值