Skip to content

Instantly share code, notes, and snippets.

@fernandoacorreia
Created February 8, 2012 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fernandoacorreia/1768915 to your computer and use it in GitHub Desktop.
Save fernandoacorreia/1768915 to your computer and use it in GitHub Desktop.
bidirectional relationships with EF Code First
using System;
namespace ConsoleApplication1.Migrations
{
using System.Data.Entity.Migrations;
internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(DatabaseContext context)
{
var usa = new Country
{
Id = new Guid("11111111-1111-1111-1111-111111111111"),
Name = "USA"
};
var brazil = new Country
{
Id = new Guid("22222222-2222-2222-2222-222222222222"),
Name = "Brazil"
};
context.Countries.AddOrUpdate(c => c.Id, usa, brazil);
var iowa = new State
{
Id = new Guid("33333333-3333-3333-3333-333333333333"),
Name = "Iowa",
CountryId = usa.Id
};
var indiana = new State
{
Id = new Guid("44444444-4444-4444-4444-444444444444"),
Name = "Indiana",
CountryId = usa.Id
};
context.States.AddOrUpdate(s => s.Id, iowa, indiana);
var desMoines = new City
{
Id = new Guid("55555555-5555-5555-5555-555555555555"),
Name = "Des Moines",
StateId = iowa.Id,
CountryId = usa.Id
};
var siouxCity = new City
{
Id = new Guid("66666666-6666-6666-6666-666666666666"),
Name = "Sioux City",
StateId = iowa.Id,
CountryId = usa.Id
};
var indianapolis = new City
{
Id = new Guid("77777777-7777-7777-7777-777777777777"),
Name = "Indianapolis",
StateId = indiana.Id,
CountryId = usa.Id
};
var terreHaute = new City
{
Id = new Guid("88888888-8888-8888-8888-888888888888"),
Name = "Terre Haute",
StateId = indiana.Id,
CountryId = usa.Id
};
var washingtonDC = new City
{
Id = new Guid("99999999-9999-9999-9999-999999999999"),
Name = "Washington, D.C.",
CountryId = usa.Id
};
context.Cities.AddOrUpdate(c => c.Id, desMoines, siouxCity, indianapolis, terreHaute, washingtonDC);
iowa.CapitalCityId = desMoines.Id;
context.States.AddOrUpdate(s => s.Id, iowa);
indiana.CapitalCityId = indianapolis.Id;
context.States.AddOrUpdate(s => s.Id, indiana);
usa.CapitalCityId = washingtonDC.Id;
context.Countries.AddOrUpdate(c => c.Id, usa);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace ConsoleApplication1
{
public class Country
{
[Required]
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public virtual ICollection<State> States { get; set; }
public virtual ICollection<City> Cities { get; set; }
[ForeignKey("Capital")]
public Guid? CapitalCityId { get; set; }
public virtual City Capital { get; set; }
}
public class State
{
[Required]
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
[Required]
public Guid CountryId { get; set; }
[Required]
public virtual Country Country { get; set; }
public virtual ICollection<City> Cities { get; set; }
[ForeignKey("Capital")]
public Guid? CapitalCityId { get; set; }
public virtual City Capital { get; set; }
}
public class City
{
[Required]
public Guid Id { get; set; }
[Required]
[MaxLength(100)]
public string Name { get; set; }
public Guid? StateId { get; set; }
public virtual State State { get; set; }
[Required]
public Guid CountryId { get; set; }
[Required]
public virtual Country Country { get; set; }
}
public class DatabaseContext : DbContext
{
public DbSet<Country> Countries { get; set; }
public DbSet<State> States { get; set; }
public DbSet<City> Cities { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<City>().HasOptional(c => c.State).WithMany(s => s.Cities);
modelBuilder.Entity<City>().HasRequired(c => c.Country).WithMany(c => c.Cities);
base.OnModelCreating(modelBuilder);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="4.3.0-beta1" />
</packages>
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
Console.WriteLine("Starting verification.");
DatabaseContext context = new DatabaseContext();
Country usa = context.Countries.Find(new Guid("11111111-1111-1111-1111-111111111111"));
Debug.Assert(usa.Name == "USA");
Debug.Assert(usa.Capital.Name == "Washington, D.C.");
City siouxCity = context.Cities.Find(new Guid("66666666-6666-6666-6666-666666666666"));
Debug.Assert(siouxCity.State.Name == "Iowa");
Debug.Assert(siouxCity.State.Capital.Name == "Des Moines");
Debug.Assert(siouxCity.State.Cities.Count == 2);
City terreHaute = context.Cities.Find(new Guid("88888888-8888-8888-8888-888888888888"));
Debug.Assert(terreHaute.State.Capital.Name == "Indianapolis");
Debug.Assert(terreHaute.State.Name == "Indiana");
Debug.Assert(terreHaute.Country.Name == "USA");
Debug.Assert(usa.States.Contains(siouxCity.State));
Debug.Assert(usa.States.Contains(terreHaute.State.Capital.State));
Console.WriteLine("Verification finished.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment