Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key example using fluent mapping #693

Open
mchomem opened this issue Nov 27, 2023 · 1 comment
Open

Foreign key example using fluent mapping #693

mchomem opened this issue Nov 27, 2023 · 1 comment

Comments

@mchomem
Copy link

mchomem commented Nov 27, 2023

@schotime or anyone who can explain.

Is there any practical example that demonstrates the use of a foreign key using NPoco's fluent mapping?

I searched the Wiki but didn't find consistent material, and here in the issues:

#232
#262

However, one was confused and the other remained unanswered in my opinion.

If you can share some expensive example, it would be of great help.

@mchomem
Copy link
Author

mchomem commented Feb 21, 2024

UP

@schotime , Is there any documentation or example for this question?

Below is the data model that I want to map to NPoco, but it is not working, the failure occurs in the Employee property, where NPoco is not being able to map, probably because I made a mistake in the configuration or not this is not possible to do:

Entity:

    public class EmployeeTask
    {
        public int Id { get; set; }
        public int EmployeeId { get; set; }
        public Employee Employee { get; set; } // Error here!
        public string Name { get; set; }
        public DateTime CreatedIn { get; set; }
        public DateTime? UpdatedIn { get; set; }
        public DateTime DeadLine { get; set; }
        public bool Done { get; set; }
    }

Mapping:

    public class EmployeeTaskMapping : Map<EmployeeTask>
    {
        public EmployeeTaskMapping()
        {
            this.TableName("EmployeeTask");
            this.PrimaryKey("Id");
            this.Columns(x =>
            {
                x.Column(c => c.Id).WithName("Id");
                x.Column(c => c.EmployeeId).WithName("EmployeeId");
                x.Column(c => c.Employee).Reference(z => z.Id, ReferenceType.Foreign);
                x.Column(c => c.Name).WithName("Name");
                x.Column(c => c.CreatedIn).WithName("CreatedIn");
                x.Column(c => c.UpdatedIn).WithName("UpdatedIn");
                x.Column(c => c.DeadLine).WithName("DeadLine");
                x.Column(c => c.Done).WithName("Done");
            });
        }
    }

What I need to know is, instead of using query with flat SQL to resolve the foreign key issue, if it is possible to use query without using flat SQL, mapping the nested object. In the case of the data layer, I tried this here:

      public async Task<List<EmployeeTask>> Retreave()
      {
              return await _context
                  .Query<EmployeeTask>()
                  .Include(x => x.Employee)
                  .ToListAsync();
      }

... but no result

As I didn't find a practical example on the Wiki, I was testing the methods within the lambda to see what can be done. If you could help with this I would be grateful.
If you need more data, just ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant