如何使用集合

How to work with collections

任何人都可以编写解释如何在 EF 中使用集合的迷你指南吗?

例如我有以下型号:

public class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
    public List<PostComment> Comments { get; set; }
}

public class PostComment
{
    public int Id { get; set; }
    public BlogPost ParentPost { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
}

和上下文class:

public class PostContext : DbContext
{
    public DbSet<BlogPost> Posts { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Posts;Trusted_Connection=True;MultipleActiveResultSets=true");

    }
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

    }
}

我需要在 OnModelCreating 方法中写什么才能在我的代码中的任何地方使用 Posts.Add 等等?

Comments导航属性似乎无法正常工作。尝试向 BlogPost 模型提供 PostComment table 的实际引用字段。还要在 PostComments 中重命名导航 属性 以符合 EF 命名规范。

public class BlogPost
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime DateTime { get; set; }

    public int PosCommentId {get; set; }
    public List<PostComment> PostComments { get; set; }
}

如果这都不起作用,您必须手动定义您的关系,但在帮助之前,您最好在您的数据库中提供这两个 table 的架构。

更新 01:

第一个提示: 您正在使用单数名称 tables。这不符合 EF 命名约定。必须关闭复数化。只需将这一行放在 OnModelCreating 方法中。

modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

第二个提示: 我之前的建议是完全错误的不要再考虑了,只需使用您的旧模型模式即可。现在,假设 comment 和 post:

之间存在一对多的关系
protected override void OnModelCreating(ModelBuilder builder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Entity<PostComment>().HasRequired<BlogPost>(c => c.ParentPost)
            .WithMany(p => p.Comments);

    base.OnModelCreating(builder);

}

更多信息:

EF one-to-many relationship tutorial

Relationship and navigation properties

Configuring Relationships with the Fluent API

以下是我在 Entity Framework Core 中使用导航属性的技巧。

技巧 1:初始化集合

class Post
{
    public int Id { get; set; }

    // Initialize to prevent NullReferenceException
    public ICollection<Comment> Comments { get; } = new List<Comment>();
}

class Comment
{
    public int Id { get; set; }
    public string User { get; set; }

    public int PostId { get; set; }
    public Post Post { get; set; }        
}

提示 2: 使用 HasOneWithManyHasManyWithOne 方法构建

protected override void OnModelCreating(ModelBuilder model)
{
    model.Entity<Post>()
        .HasMany(p => p.Comments).WithOne(c => c.Post)
        .HasForeignKey(c => c.PostId);
}

技巧 3: 预先加载集合

var posts = db.Posts.Include(p => p.Comments);

技巧 4:如果您不急于加载,请显式加载

db.Comments.Where(c => c.PostId == post.Id).Load();

为了补充@FabioCarello 和@bricela 的回答,我建议在 Navigation Properties 上使用 virtual 关键字:

public virtual ICollection<Comment> { get; set; }

这将允许 延迟加载,这意味着 collections/references 将仅在首次调用时加载,而不是在首次数据检索期间加载。这对于避免例如递归引用时的堆栈溢出非常有用。

没有 Fluent 对于像您这样的简单 一对多 关系是必需的,除非您需要非标准行为,例如明确要求不可为 null 的引用。

您还可以显式使用 外键 这很有用,这样当您只需要其 Id[= 时就不必加载父项27=]:

public class PostComment
{
    public int Id { get; set; }
    public BlogPost ParentPost { get; set; }
    // //
    public int? ParentPostId { get; set; }
    // //
    public string Content { get; set; }
    public DateTime DateTime { get; set; }
}