简体   繁体   中英

Entity Framework Foreign Key Select

I have a model

Retailer which has the fields

ID

Name

and another

Store

StoreName

RetailerID

In SQL to get the name of the retailer rather than just the ID when selecting all stores I can use a join.

How do I go about this in EF??

using (var context = new DBContext())
{    
//Get all stores with the name of the retailer
}

Thanks

You can simply access the related Retailer object on the Store object itself, for example:

using(var context = new DBContext())
{
    var myStore = context.Stores.First(s => s.StoreName == "my store");
    string retailerName = myStore.Retailer.Name;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM