Analog of methods "reference("field").Load()" and "Include(p => p.field)" for an instance originally created not from the base?

Viewed 12

An instance of the class is returned from the view. I need to find out the reference fields of a class that are in the database, but not in the view. How can I get them?

class User {
    guid id {get; set;}
    public List<LinkUserOrganization> AllowedOrganizations { get; set; }
}

public class LinkUserOrganization
{
    public Guid UserId { get; set; }
    public User User { get; set; }
    public Guid OrganizationId { get; set; }
    public Organization Organization { get; set; }
}

[HttpPost]
public ActionResult Edit(User user)
{
    foreach (LinkUserOrganization oldlink in user.AllowedOrganizations) // There is nothing here, although there is a database
}

UPD temporarily so...

User oldRecord = db.Users.AsNoTracking().Include(u => u.AllowedOrganizations).FirstOrDefault(x => x.Id == user.Id);
            foreach (LinkUserOrganization oldlink in oldRecord.AllowedOrganizations)
            {
                if (AllowedOrganizationIdentifiers.Contains(oldlink.OrganizationId))
                {
                    db.Entry(oldlink).State = EntityState.Unchanged;
                    AllowedOrganizationIdentifiers.Remove(oldlink.OrganizationId);
                }
                else
                {
                    db.Entry(oldlink).State = EntityState.Deleted;
                }
            }
0 Answers
Related