schotime / NPoco

Simple microORM that maps the results of a query onto a POCO object. Project based on Schotime's branch of PetaPoco
Apache License 2.0
848 stars 302 forks source link

Column Name attribute does not map properly on Nested Objects #674

Open Oskar-Ardolo opened 1 year ago

Oskar-Ardolo commented 1 year ago

Hi, I'm currently learning how to use NPoco and I have a strange behavior, I can't figure out what am I doing wrong with this. Any help is greatly appreciated.

My database is designed like this :

Agency (Id, AgencyName) User (Id, Name, Agency) where Agency column references Agency.Id

I made two classes:

Agency class

[TableName("Agency")]
[PrimaryKey("Id", AutoIncrement = true)]
class Agence
{
    [Column]
    public int Id { get; set; }

    [Column(Name = "AgencyName")]
    public string Name { get; set; }
}

User class

[TableName("User")]
[PrimaryKey("Id", AutoIncrement = true)]
[ExplicitColumns]
class User
{
    [Column]
    public int Id { get; set; }

    [Column]
    public string Name { get; set; }

    [ComplexMapping]
    [ResultColumn]
    public Agency Agency { get; set; }
}

The only noticeable thing here is that the Name property in Agency class is not the same as the database field name (AgencyName).

I am retrieving a user this way:

User user = db.Single<User>(@"select [User].Name, [User].Id, [Agency].Id, [Agency].AgencyName 
                                    FROM [User], [Agency] 
                                    WHERE [User].Id = @0 
                                    AND [Agency].Id = [User].Agency", 1);

With this line of code, every members from User and Agency are properly populated with correct values except Name property from Agency class. It is always null.

It seems like I have two options to make the "Name" field properly filled :

Those 2 ways are correctly filling the property with correct data, but in my understanding, I should not have to make those changes since I'm already adding [Column(Name = "AgencyName")] attribute to the property.

Am I missing something?

Thank you very much for your help. Regards