【发布时间】:2017-11-23 18:20:22
【问题描述】:
因此,我目前正在尝试使用 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);
}
}
【问题讨论】:
标签: c# sql entity-framework asp.net-core asp.net-identity