Skip to content

Instantly share code, notes, and snippets.

@JerryBian
Created July 7, 2014 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JerryBian/8ed6605453adaed579b2 to your computer and use it in GitHub Desktop.
Save JerryBian/8ed6605453adaed579b2 to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
var context = new TestContext();
context.Parents.Add(new Parent
{
Name = "Parent",
Child1s = new Collection<Child1>
{
new Child1
{
Child2 = new Child2()
{
Name = "Child2"
},
Name = "Child1"
}
}
});
context.SaveChanges();
Console.ReadKey();
}
public class Parent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Child1> Child1s { get; set; }
}
public class Child1
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public Child2 Child2 { get; set; }
public Parent Parent { get; set; }
}
public class Child2
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
public string Name { get; set; }
public Child1 Child1 { get; set; }
}
public class TestContext : DbContext
{
static TestContext()
{
AppDomain.CurrentDomain.SetData("DataDirectory", @"E:\");
Database.SetInitializer(new MigrateDatabaseToLatestVersion<TestContext, Migrations.Configuration>());
}
public DbSet<Parent> Parents { get; set; }
public TestContext()
: base(
@"Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\CloudComputingDB-20140610.mdf;Initial Catalog=CloudComputingDB-20140610;Integrated Security=True;MultipleActiveResultSets=True;"
)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Child1>().HasRequired(c1 => c1.Child2).WithRequiredPrincipal(c2 => c2.Child1);
base.OnModelCreating(modelBuilder);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment