Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Entity of foreign key not loaded after create? #3042


User avatar
0
DennisAhlin created

Hi!

My classes:

public class Member: FullAuditedEntity, IMustHaveTenant
    {
        [Required]
        public int TenantId { get; set; }

        [ForeignKey("CompanyId")]
        public virtual Company Company { get; set; }
        public virtual int? CompanyId { get; set; }
    }
public class Company : FullAuditedEntity
    {
        [Required]
        public string Title { get; set; }
        public virtual ICollection<Member> Members { get; set; }
    }

I want the list of all members after adding a member to a company, like this:

...
newMember.CompanyId = 12;
await _repository.InsertAsync(newMember);
await CurrentUnitOfWork.SaveChangesAsync();
var membersOfCompany = newMember.Company?.Members;
...

But the newMember.Company is null after the save changes? How can I load the cooperation entity to the new member?


1 Answer(s)
  • User Avatar
    0
    alirizaadiyahsi created

    Hi,

    As far as I know EF does not support this usage. You can search on web if there is a solution.