Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

EF Populate Inverse Navigation Entity

$
0
0

I'm trying populate an object using EF Core that has an inverse relationship. I have a table "Party" with a child table "PartyEntertainment" that has a collection of entertainers. There is a field in Entertainment called EntertainmentTypeId that references a lookup table EntertainmentType (-> EntertainmentTypeId int, EntertainmentTypeDisplay varchar(16)) .  I populate the data using:

var party = await _context.Party
	.Include(h => h.Host)
	.Include(t => t.PartyTopic)
	.Include(e => e.PartyEntertainment)
	.Include(g => g.GuestList)
	.FirstOrDefaultAsync();

The PartyEntertainment object has a property of type EntertainmentType for the inverse relationhip on EntertaimentType object, but it is null. How can I populate this?

In my View I want to show it using

@foreach (var e in party.PartyEntertainment) {<div><span>@e.Name</span><span>@e.EntertainmentType.EntertainmentTypeDisplay</span><span>@e.Description</span></div>
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>