Skip to content

Instantly share code, notes, and snippets.

@bradymholt
Last active August 29, 2015 14:07
Show Gist options
  • Save bradymholt/85d348e9bdf2d1823e47 to your computer and use it in GitHub Desktop.
Save bradymholt/85d348e9bdf2d1823e47 to your computer and use it in GitHub Desktop.
Entity Framework One-to-One Relationship
public class DataContext : DbContext
{
public DbSet<Order> Orders { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
}
public class Order
{
public int Id { get; set; }
public string Name { get; set; }
public virtual OrderDetail Detail { get; set; }
}
public class OrderDetail
{
[Key]
[ForeignKey("Order")] //[ForeignKey] required for 1:1
public int OrderId { get; set; }
public Order Order { get; set; } //Navigation property required for 1:1
public string ReferenceNumber { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment