A
A
Andrey2019-12-26 17:33:33
ASP.NET
Andrey, 2019-12-26 17:33:33

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);
        }

It is not possible to refer to the data loading on the AttachedFile entity. I get an exception that the reference does not point to an instance of an object.
I'm using ASP.NET Core 2.1 + EF
Thanks!
UPD!
employees
------------------------------------------------ --
| id | fio | position | Bythday |
1 Tester O.O. 2 01/01/2010
2 Testers D.D 3 06/02/2000
positions
--------------------
id employees file posname
1 1 1 Manager
2 1 3 Senior Manager
3 2 2 Accountant
4 2 4 Senior Accountant
files
-----------------------------
id filename
1 Order #1.docx
2 Order #341.docx
3 Order #3.docx
4 Order No. 167.docx
Required option for displaying data.
Table "Career growth" of employee Tester O.O.
Position Scan of the order
Manager Order No.
1.docx Senior Manager Order No. 3.docx
5e07368084683081117134.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2019-12-26
@kttotto

one.

There is just a number that corresponds to the id in the Scan table.

Where is the number, in what table? Is there any connection between the employee and the contract? Otherwise, it will simply be impossible to correlate the contract record with the employee record.
2. Not the best decision to give the entity entity to the view. It's better to add a DetailsViewModel that you fill with data however you want. for example
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);

R
Roman, 2019-12-27
@yarosroman

public AttachedFile? File .... In this way, declare the field, most likely that's why, the FileId field is automatically created for you and it is not nullable, so the exception most likely pops up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question