Can anyone write mini-guide which explains how to work with collections in EF?
For example I have following models:
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; }
}
And context 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);
}
}
What do I need to write in OnModelCreating method so that I can use Posts.Add and etc. everywhere in my code?
See Question&Answers more detail:os