これは1:0..1と思う。従業員はEmployeeOptions従業員を持っている必要があり、従業員のテーブルを移動することによって放置され、EmployeeOptionsを有していてもよい:
public class Employee
{
[Key]
public int EmpId { get; set; }
public string FName { get; set; }
public string LName { get; set; }
[ForeignKey("EmpId")]
public virtual EmployeeOption EmployeeOption { get; set; }
}
public class EmployeeOption
{
[Key]
public int EmpId { get; set; }
public string Option1 { get; set; }
public string Option2 { get; set; }
[ForeignKey("EmpId")]
public virtual Employee Employee { get; set; }
}
public class ExampleContext : DbContext
{
public ExampleContext() : base("DefaultConnection") { this.Configuration.ProxyCreationEnabled = false; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.HasOptional(o => o.EmployeeOption)
.WithOptionalPrincipal(e => e.Employee);
}
public DbSet<Employee> Employees { get; set; }
public DbSet<EmployeeOption> EmployeeOptions { get; set; }
}
生成テーブル(マイグレーション):
CreateTable(
"dbo.EmployeeOptions",
c => new
{
EmpId = c.Int(nullable: false),
Option1 = c.String(),
Option2 = c.String(),
})
.PrimaryKey(t => t.EmpId)
.ForeignKey("dbo.Employees", t => t.EmpId)
.Index(t => t.EmpId);
CreateTable(
"dbo.Employees",
c => new
{
EmpId = c.Int(nullable: false, identity: true),
FName = c.String(),
LName = c.String(),
})
.PrimaryKey(t => t.EmpId);
EDIT:以下を使用して上記のものの代わりに流暢なマッピングを使用すると、[ForeignKey]属性を削除できます:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<EmployeeOption>()
.HasRequired(e => e.Employee)
.WithOptional(e => e.EmployeeOption);
}
'WithOptionalPrincipal'の代わりに' WithRequired'を試してみてください。 –