在 EF 6 中找不到 HasOne
Posted
技术标签:
【中文标题】在 EF 6 中找不到 HasOne【英文标题】:HasOne not found in EF 6 【发布时间】: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 和 *** 上找到它,但我发现的唯一内容是如何使用它,而不是为什么它会出现问题。我真的错过了参考吗?如果有,是哪一个?
【问题讨论】:
【参考方案1】:HasOne()
是 Entity Framework Core 方法。
在以前的版本中,您使用HasOptional()
or HasRequired()
。
【讨论】:
以上是关于在 EF 6 中找不到 HasOne的主要内容,如果未能解决你的问题,请参考以下文章