public class Context : DbContext { public DbSet<Box> Boxes { get; set; } public DbSet<Crank> Cranks { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Box>() .HasRequired(m => m.Crank) .WithOptional() .Map(m => m.MapKey("Crank_Id")); base.OnModelCreating(modelBuilder); } } public class Box { public int Id { get; set; } public Crank Crank { get; set; }
You do not need to specify the following:
HasKey(t => t.Id); ToTable("Boxes") Property(t=>t.Id).HasColumnName("Id") Property(t=>t.CrankId).HasColumnName("Crank_Id") HasRequired(t=>t.Crank)
It will be discovered by agreement of the EF.
source share