EntityFrameworkCore: how to reverse lookup entities with ForeignKey?

asp.net-core asp.net-mvc c# entity-framework-core

Accepted Answer

ZZZ_tmp
1
6/13/2017 4:43:01 AM

Popular Answer

Example:

   public class Org : BaseEntity, IEntityBase
    {
        public string Name { get; set; }
        public ICollection<Portfolio> Portfolios  { get; set; }
    }

    public class Portfolio : BaseEntity, IEntityBase
    {
        public string Name { get; set; }
        public int OrganizationId { get; set; }

        [ForeignKey("OrganizationId")]
        public virtual Org Organization { get; set; }
        public bool IsPrivate { get; set; }
    }

Use:

var orgs= _context.Portfolios.Include(a=>a.Organization).select(s=>s.Organization);
var portfolios= _context.Orgs .Include(a=>a.Portfolios).SelectMany(s=>s.Portfolios);


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