I have 2 entities in my database that have a one-to-many relationship.
The principal table is called Items
, the related table is called Jobs
.
An Item
can have 0 ... many Jobs
. A job is only related to a single item.
The EF Core entities look like this (simplified):
public class Item
{
public Guid Id { get; set; }
public virtual ICollection<Job> Jobs { get; set; }
}
public class Job
{
public Guid Id { get; set; }
public Guid ItemId { get; set; }
public virtual Item Item { get; set; }
}
For the Items
table, I have created a view (called ItemView
) with some additional data I need.
ItemView
is a readonly abstraction based on Item and some other information in the database. It's only used to simplify queries from the application.
The relationship to the Jobs
table however stays the same.
Is it possible in EF Core 3.1 to define the same relationship between Item
and Jobs
also for the ItemView
to make use of includes in the queries and have EF generate nested objects?
I would like to be able to have a query like
var result = await context.ItemView.Include(i => i.Jobs)
and get back the same nested objects (but containing my additional properties in ItemView
) I would get with
var result = await context.Item.Include(i => i.Jobs)