因此,我目前正在尝试使用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<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);
}
}
https://stackoverflow.com/questions/44666042
复制相似问题