Ken*_*man 6 c# entity-framework ef-fluent-api
我是Entity Framework的新手,我正试图找出关系.我找到了这段代码:
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; }
}
Run Code Online (Sandbox Code Playgroud)
编译代码时出错:
'EntityTypeConfiguration'不包含'HasOne'的定义,也没有扩展方法'HasOne'接受类型'EntityTypeConfiguration'的第一个参数(你是否缺少using指令或汇编引用?)
我试图在谷歌和StackOverflow上找到它,但我发现的唯一的事情是如何使用它而不是它给出问题的原因.我真的错过了参考吗?如果是这样,哪一个?