NPoco icon indicating copy to clipboard operation
NPoco copied to clipboard

Column Name attribute does not map properly on Nested Objects

Open Oskar-Ardolo opened this issue 3 years ago • 2 comments

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 :

  • Changing the "AgencyName" property to "Name" to make it same as Database column name
  • Adding "as Name" to [Agency].AgencyName field in the SQL Query

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

Oskar-Ardolo avatar Nov 17 '22 14:11 Oskar-Ardolo

Either

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

or

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

schotime avatar Feb 25 '25 22:02 schotime

Either

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

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

Hi, thanks for your reply! This is where I do not really understand. As stated in question, I figured out that using as Name in SQL query solved my problem. But in that case, what is the purpose of the [Column(Name = "AgencyName")] attribute?

Thanks

Oskar-Ardolo avatar Mar 07 '25 10:03 Oskar-Ardolo