SCJP1.6 PWR
SCJP1.6 PWR

Reputation: 371

foreign key missing - Entity framework code first

I am using Entity framework code first framework. I am creating foreing keys. But when I am accessing data from the table, I am only table information without foreign key information.

public class X : EntityData {
    public int a;
    public int b;
    public ICollection<Y> YID { get; set; } //This will create a foreign key of X_ID in Y table
}

public class Y : EntityData {
    public int c;
    public int d;
}

Now, if I try to access Y table from front end, it doesn't return me foreign key of that is X_ID. But it is returning c and d. In sql table, I can see X_ID in Y table as foreign key. Please let me know the best way to get foreign key table information from server along with client. But help link
http://myapp.azure-mobile.net/help/Api/GET-tables-Y shows foreign key details with the table but actual service is not returning foreign key details not even foreign key id.
Thanks in Advance.

Upvotes: 0

Views: 726

Answers (1)

Franz Wimmer
Franz Wimmer

Reputation: 1487

You have to define the foreign key relationship in your Y class like this (and you might want to have a primary key in your X class):

public class X : EntityData {
    [Key]
    public int a { get; set; }
    public int b { get; set; }
    public ICollection<Y> YID { get; set; } //This will create a foreign key of X_ID in Y table
}

public class Y : EntityData {
    public int c { get; set; }
    public int d { get; set; }

    public int XId { get; set; }
    [ForeignKey("XId")]
    public virtual X XReference { get; set; }
}

Upvotes: 1

Related Questions