简体   繁体   English

EF CTP4生成的表中缺少列

[英]EF CTP4 Missing columns in generated table

I'm having an issue that i just can't seem to figure out. 我有一个我似乎无法解决的问题。 Lets say I have 2 Entities defined in my domain; 假设我在域中定义了2个实体; Person and Document. 人和文件。 Below is the definition for Document : 以下是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. 现在,当EF CTP4在初始化时创建SQL表时,只有一个字段映射到Person_Id为Owner_id。 Whatever i try, the field for AssignedTo is never created. 无论我如何尝试,都不会创建AssignedTo的字段。

Anything that could solve this? 有什么可以解决这个问题的吗?

Regards, 问候,

avsomeren 阿夫索伦

Your code perfectly created the desired schema in the database for me: 您的代码为我完美地在数据库中创建了所需的架构: 替代文字

If you don't get this schema in you DB then my guess is that something is not right with the rest of your object model. 如果您没有在数据库中获得此​​架构,那么我的猜测是其余的对象模型不正确。 Could you post your full object model please? 您可以发布完整的对象模型吗?

Another Solution: 另一个解决方案:

While your current Document class will give you the desired results, but you can still take advantage of the Conventions for Code First and explicitly specify the FKs for your navigation properties: 虽然您当前的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 will now infer that any property named <navigation property name><primary key property name> (eg OwnerID), with the same data type as the primary key (int), represents a foreign key for the relationship. 现在,“代码优先”将推断出名为<导航属性名称> <主键属性名称> (例如OwnerID)的任何属性,其数据类型与主键(int)相同,表示该关系的外键。

This essentially results to the same DB schema plus you have the FKs on your Document object as well as navigation properties which gives you ultimate flexibility to work with your model. 这实际上导致了相同的数据库模式,此外,您在Document对象上具有FK以及导航属性,这为您提供了极大的灵活性来处理模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM