1

I'm having an issue that i just can't seem to figure out. Lets say I have 2 Entities defined in my domain; Person and Document. Below is the definition for Document :

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; }
}

Now, when EF CTP4 creates the SQL table on initialize, there is only one field mapping to a Person.Id being Owner_id. Whatever i try, the field for AssignedTo is never created.

Anything that could solve this?

Regards,

avsomeren

4

1 回答 1

0

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

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

另一个解决方案:

虽然您当前的 Document 类将为您提供所需的结果,但您仍然可以利用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 以及导航属性,这为您提供了使用模型的最大灵活性。

于 2010-11-26T21:14:02.910 回答