Friday, March 1, 2019

Entityframework eager loading.

Are there any other way to load the related objects of my class, specially the Address? I am new to EF and did some research and cant find a similar case, I red about Lazy Loading but suggest to avoid it if your building a Web Application.

Here's the code to retrieve a single company

_context.Companies

.Include(c=>c.Address.Province)

.Include(c=>c.Address.CityMunicipality)

.Include(c=>c.Address.Town)

.Include(c=>c.ContactInformation)

.Where(c => c.CompanyId == Id).FirstOrDefault();

I have a class of Company that has relation To Address Class, Address class has a navigation Property of Province, CityMunicipality and Town.

public class Company

{

public int CompanyId { get; set; }

public string CompanyCode { get; set; }

public string CompanyName { get; set; }

public virtual ContactInformation ContactInformation{ get; set; }

public virtual Address Address{ get; set; }

}

public class Address

{

public int Id { get; set; }

[ForeignKey("Company")]

public int? CompanyId { get; set; }

public virtual Company Company { get; set; }

[ForeignKey("Province")]

public int ProvinceId { get; set; }

public virtual Province Province { get; set; }

[ForeignKey("CityMunicipality")]

public int CityMunicipalityId { get; set; }

public virtual CityMunicipality CityMunicipality { get; set; }

[ForeignKey("Town")]

public int TownId { get; set; }

public virtual Town Town { get; set; }

public string Street { get; set; }

}

Entityframework eager loading. Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team