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

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

Open
Oskar-Ardolo opened this issue Nov 17, 2022 · 0 comments
Open

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

Oskar-Ardolo opened this issue Nov 17, 2022 · 0 comments

Comments

@Oskar-Ardolo
Copy link

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

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