I have Entity Framework First DbContext code with settings for the following objects. In this example, the Bar class is a child of the Foo class.
public class Foo { public Guid Id { get; set; } public virtual ICollection<Bar> Bars { get; set; } } public class Bar { public Guid Id { get; set; } public Guid FooId { get; set; } public virtual Foo Foo { get; set; } }
Now I know that internally, the Entity Framework understands that the relationship between Foo and Bar is determined by the foreign key of Bar.FooId. What I would like to do is somehow extract this relationship at runtime using expressions. I would like to implement a method that behaves as follows:
var context = new FooBarDbContext(); var bar = context.Set<Bar>().First();
Now in this simplified example, I know that I can just get bar.FooId and do it. The fact is that I am writing a class for which I believe that the GetForeignKeyValue method mentioned above is the cleanest user interface.
Is it possible to query the DbContext configuration to determine which property is used as a foreign key for the navigation property? (Suppose there is one)
source share