Code First实体之间的映射关系

时间:2014-11-18 12:22:12

标签: c# entity-framework ef-code-first

拥有这两个实体:

public partial class Title
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public partial class Person
{
    public int Id { get; set; }
    public string Description { get; set; }
    public virtual Title Title { get; set; }
}

如何映射关系以便在Person中需要Title?

1 个答案:

答案 0 :(得分:0)

您可以使用Fluent API

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Person>()
                .HasRequired(a => a.Title);
}