Entity Framework - get related items without explicit reference using Fluent API configurations

api c# entity-framework-6 fluent

Question

Let's say I have database table A with optional property X. I have table B with a composite key (Y, X). These two tables are not explicitly related in any way.

e.g.

create table A
(
    Id uniqueidentifier not null,
    X uniqueidentifier null
)

create table B
(
    Y uniqueidentifier not null,
    X uniqueidentifier not null,
    Constraint PK_B Primary Key (Y, X)
)

A and B are implicitly related via property X, in the sense that X is an orphan id (its base table has been moved out)

For my model A in C# code, how can I populate IList<B> using property X to get all items in B containing X? Ideally, I'd like to use Fluent API configurations.

e.g

public class A {
    public Guid Id {get; set;}
    public Guid? X {get; set;}
    public IList<B> RelatedItems {get; set;}
}

public class B {
    public Guid X {get; set;}
    public Guid Y {get; set;}
}

How do I configure my entities using Fluent API so that I can use something like

var list = await context.AQuery()
.Include(a => a.RelatedItems)
.ToListAsync().ConfigureAwait(false);

or any other way to populate IList in A

Thanks in advance!

1
1
8/19/2018 8:24:08 PM

Popular Answer

in ur model a x is a property , a property has a getter , u can set the get method of the x property to retrieve the records from the database (similar to the lazy loading design pattern )

public IList<SomeStuff> X {
get { // go get records from database }
}

this will cost u an extra so i am not sure if this is what u want

0
8/19/2018 4:46:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow