avsomeren
avsomeren

Reputation: 11

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

Upvotes: 1

Views: 227

Answers (1)

Morteza Manavi
Morteza Manavi

Reputation: 33216

Your code perfectly created the desired schema in the database for me: alt text

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:

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> (e.g. OwnerID), with the same data type as the primary key (int), represents a foreign key for the relationship.

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.

Upvotes: 0

Related Questions