EF CTP4生成的表中缺少列

时间:2010-11-26 20:02:26

标签: entity-framework ctp4

我遇到了一个我似乎无法弄清楚的问题。假设我在我的域中定义了2个实体;人与文件。以下是文档的定义:

public class Document
{
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public string Title { get; set; }
    public DateTime Date { get; set; }

    public virtual Person Owner{ get; set; }
    public virtual Person AssignedTo { get; set; }
}

现在,当EF CTP4在初始化时创建SQL表时,只有一个字段映射到Person.Id是Owner_id。无论我尝试什么,AssignedTo的字段永远不会创建。

有什么能解决这个问题吗?

此致

avsomeren

1 个答案:

答案 0 :(得分:0)

您的代码为我完美地在数据库中创建了所需的架构: alt text

如果你没有在你的数据库中获得这个模式,那么我的猜测是你的对象模型的其余部分是不正确的。你能发布完整的对象模型吗?

另一种解决方案:

虽然您当前的Document类将为您提供所需的结果,但您仍然可以利用Conventions for Code First并明确指定导航属性的FK:

public class Document
{
    public int Id { get; set; }
    [Required][StringLength(255)]
    public string Title { get; set; }
    public DateTime Date { get; set; }

    public int OwnerID { get; set; }
    public int AssignedToID { get; set; }

    public virtual Person Owner { get; set; }
    public virtual Person AssignedTo { get; set; }

}

Code First现在将推断出任何名为< navigation property name>< primary key property name> (例如OwnerID)的属性,其数据类型与主键(int)相同,代表关系的外键。

这实际上导致了相同的数据库模式,并且您在Document对象上拥有FK以及导航属性,这为您提供了使用模型的最大灵活性。

相关问题