你应该使用这个:
计划班
public virtual Material Material { get; set; }
public int? MaterialId { get; set; } /* it prompt clr create foreign key MatherialId which referenced to MatherialId class of Matherial */
数学课
public int MaterialId { get; set; }
更新
完整的解决方案
实体:
namespace MvcApplicationTest.Models
{
public class Material
{
public int MaterialId { get; set; }
public int Name { get; set; }
}
public class Plan
{
public int PlanId { get; set; }
public int Name { get; set; }
//full navigation property
public virtual Material Material { get; set; }
public int? MaterialId { get; set; }
//
}
public class TestContext : DbContext
{
public DbSet<Material> Materials { get; set; }
public DbSet<Plan> Plans { get; set; }
}
}
global.asax 中的一些初始化:
var context = new TestContext();
context.Database.CreateIfNotExists();
结果应该是这样的:
对于你的名字:
[ForeignKey("MaterialFK")]
public virtual Material MyMaterial { get; set; } //your name
public int? MaterialFK { get; set; } //your name