CSharp · Console
// @nuget: EntityFramework
using System;
using System.Data.Entity;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
public class Program
{
public static void Main()
{
using (var context = new BookStore())
{
Author author = new Author()
{
Name = "Mark",
Biography = new AuthorBiography()
{
Biography = "Biography",
DateOfBirth = DateTime.Now,
PlaceOfBirth = "NY",
Nationality = "US"
}
};
context.Authors.Add(author);
context.SaveChanges();
var authorData = context.Authors.ToList();
var authorBio = context.AuthorBiographies.ToList();
FiddleHelper.WriteTable(authorData);
FiddleHelper.WriteTable(authorBio);
}
}
public class BookStore : DbContext
{
public BookStore() : base(FiddleHelper.GetConnectionStringSqlServer())
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
public DbSet<Author> Authors { get; set; }
public DbSet<AuthorBiography> AuthorBiographies { get; set; }
}
public class AuthorBiography
{
[ForeignKey("Author")]
public int AuthorBiographyId { get; set; }
public string Biography { get; set; }
public DateTime DateOfBirth { get; set; }
public string PlaceOfBirth { get; set; }
public string Nationality { get; set; }
public virtual Author Author { get; set; }
}
public class Author
{
public int AuthorId { get; set; }
public string Name { get; set; }
public virtual AuthorBiography Biography { get; set; }
}
}