maxtoroq / DbExtensions

Data-access framework with a strong focus on query composition, granularity and code aesthetics.
https://maxtoroq.github.io/DbExtensions/
Apache License 2.0
227 stars 41 forks source link

ColumnAttribute Name does not work #81

Closed EthemAcar-Dev closed 2 years ago

EthemAcar-Dev commented 2 years ago

public class MyTableName { [Column(Name = "nOffer")] public int Offer { get; set; } }

This is my entity model. When I Map my query using DBExtensions I get 100 results back but the values are all 0.

If i change the property to table name like:

[Column(Name = "nOffer")] public int nOffer { get; set; }

Then i get correct values.

maxtoroq commented 2 years ago

How are you building the query? If you are using SqlBuilder or a plain string, you are responsible for the column to property mapping.

EthemAcar-Dev commented 2 years ago

var query = SQL.SELECT("TOP (1) *").FROM("MyTableName"); var execution = _db.Map<MyTableName>(query)?.First();

Isn't this the correct way?

maxtoroq commented 2 years ago

Try this:

var execution = _db.Table<MyTableName>()
    .First();
EthemAcar-Dev commented 2 years ago

Try this:

var execution = _db.Table<MyTableName>()
    .First();

Gives the same result back as my previous code. Seems like Column name property is not working correctly? Which is bad for me because there are some columns that start with numbers on my database and C# does not accept numbers as property names.

[Column(Name = "ColumnName")]

maxtoroq commented 2 years ago

Are you using the Table attribute on the entity class?

EthemAcar-Dev commented 2 years ago

Are you using the Table attribute on the entity class?

 [Table(Name = "MyTableName")]
 public class MyTableName
 {
 [Column(Name = "nOffer")]
public int Offer { get; set; }
 }

Yeah like this.

This code :

 var query = SQL.SELECT("TOP (1) *").FROM("MyTableName"); 
 var execution = _db.Map<MyTableName>(query)?.First();

and this one:

 var execution = _db.Table<MyTableName>().First();

Returns a row but the Offer parameter is value 0. In the database nOffer value is 257.

maxtoroq commented 2 years ago

I cannot reproduce the issue. If you call _db.Table<MyTableName>().GetDefiningQuery().ToString() what do you get? That way we can find out if it's using the column name or not.

EthemAcar-Dev commented 2 years ago

Aha my bad it all makes sense right now.

DbExtensions uses the given Column name value to build SQL like:

SELECT [nOffer] AS [Offer]
FROM [MyTableName]

So if i build a query using DbExtensions SQL object i should type it as follow:

SQL.Select("TOP 1 [nOffer] AS [Offer]"); 

Which returns the correct database value.

Thank you very much for the support.