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