0

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.

1 Answer 1

1

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

ForeignKey annotation is not found. It gives compilation error in visual basic.
The ForeignKey attribute is located in the namespace System.ComponentModel.DataAnnotations.Schema. Did you import it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.