Answer the question
In order to leave comments, you need to log in
EF Core Loading three or more entities. How right?
Good afternoon! Please tell me, purely theoretically, is it possible to load a set of data connected with an entity without using a binding entity?
It turned out that there is a project with a database on mysql and I cannot make changes to the structure of the database. :(
I have an Employee, I have an employee's Positions, and there are scans of orders for appointment to an employee's position. To connect the Employee and the Employee's Position, a foreign key is used. And between the Position and the Scan of the order, there is no connection by a foreign key. id in the Scan table.And
for some reason it is not possible to make this connection not through Annotations not through the Fluent
API.When building a model, the data about the attached Scan is not loaded.I
ask for your help.
public class Employee
{
[Key, ForeignKey("employee")]
public long Id { get; set; }
public string FIO {get; set;}
[ForeignKey("position")]
public Position Position { get; set; }
public DateTime Bithday { get; set; }
}
public class Position
{
[Key]
public long Id { get; set; }
[ForeignKey("employee ")]
public Employee Employee { get; set; }
public AttachedFile File { get; set; }
// UPD
public string PosName { get; set; }
}
public class AttachedFile
{
[Key]
public long Id { get; set; }
public string FileName { get; set; }
}
Загружаю данные для передачи в представление:
public IActionResult Details(int id)
{
var model = db.employees
.Include(ep => ep.Position)
.FirstOrDefault(j => j.Id == id)
;
return View(model);
}
Answer the question
In order to leave comments, you need to log in
one.
There is just a number that corresponds to the id in the Scan table.
var detailsViewModel = db.employees.Include(ep => ep.Position)
.Where(j => j.Id == id)
.Select(emp => new EmploeeViewModel
{
Name = emp.Name,
Position = emp.Position,
Сontract = db.Contract.FirstOrDefault(c => c.Id == emp.ContractId)
}.FirstOrDefault();
return View(detailsViewModel);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question