【发布时间】:2017-11-12 21:09:10
【问题描述】:
我对实体框架非常陌生,我正在尝试找出关系。我找到了这段代码:
class MyContext : DbContext
{
public DbSet<Post> Posts { get; set; }
public DbSet<Tag> Tags { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PostTag>()
.HasKey(t => new { t.PostId, t.TagId });
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Post)
.WithMany(p => p.PostTags)
.HasForeignKey(pt => pt.PostId);
modelBuilder.Entity<PostTag>()
.HasOne(pt => pt.Tag)
.WithMany(t => t.PostTags)
.HasForeignKey(pt => pt.TagId);
}
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class Tag
{
public string TagId { get; set; }
public List<PostTag> PostTags { get; set; }
}
public class PostTag
{
public int PostId { get; set; }
public Post Post { get; set; }
public string TagId { get; set; }
public Tag Tag { get; set; }
}
编译代码时出现错误:
“EntityTypeConfiguration”不包含定义 'HasOne' 并且没有扩展方法 'HasOne' 接受第一个参数 可以找到“EntityTypeConfiguration”类型的(你是 缺少 using 指令或程序集引用?)
我曾尝试在 Google 和 StackOverflow 上找到它,但我发现的唯一内容是如何使用它,而不是为什么它会出现问题。我真的错过了参考吗?如果有,是哪一个?
【问题讨论】:
标签: c# entity-framework ef-fluent-api