public class Blog { public int BlogId { get; set; } public string Name { get; set; } public virtual List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public virtual Blog Blog { get; set; } }
public class BloggingContext : DbContext { public BloggingContext() : base("MySqlServer") { } public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } }
class Program { static void Main(string[] args) { using (BloggingContext db = new BloggingContext()) { db.Blogs.Add(new Blog { Name = "Another Blog" }); db.SaveChanges(); foreach (var blog in db.Blogs) { Console.WriteLine(blog.Name); } } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }