1

To simplify the case, let's say I have the following table definitions in Oracle:

create table customer
(
    id   number(10),
    name nvarchar2(1024),
    primary key (id)
);

create table customer_order
(
    id          number(10),
    customer_id number(10),
    name        nvarchar2(1024),
    primary key (id),
    foreign key (customer_id) references customer(id)
);

And I have the following classes for the database objects:

public class Customer
{
    public int ID { get; set; }
    public string? Name { get; set; }

    public List<CustomerOrder> CustomerOrders { get; set; } = new List<CustomerOrder>();
}

public class CustomerOrder
{
    public int ID { get; set; }
    public string? Name { get; set; }
}

Let's also say I want to get back a list of customers, with each customer object having a list of CustomerOrders, using the following SQL:

select      
    a.id customer_id, a.name customer_name, 
    b.id customer_order_id, b.name customer_order_name
from        
    customer a     
inner join      
    customer_order b on  a.id = b.customer_id
order by    
    a.id, b.id

As both tables (customer and customer_order) have the same columns ID and NAME, I need to change them to XXX_ID and XXX_NAME (where XXX = CUSTOMER or CUSTOMER_ORDER); otherwise, Oracle will change them to ID_1, NAME_1, etc. (if there are duplicate column names in the result).

Also, both C# classes (Customer and CustomerOrder) have the same properties ID and Name as well.

We want the database and C# class definition to be clean and not wordy (the ID in Customer is a customer ID and there is no need to explicitly say it).

As Dapper maps database columns to C# properties by either constructor parameter name or property name, how this can be mapped by Dapper automatically or what is the best way to do this in Dapper?

Even I can change the column name in the SQL query level (ID to CUSTOMER_ID, etc.), the C# class property name cannot be changed like that. I tried to use System.ComponentModel.DataAnnotations.Schema.ColumnAttribute with both the upper (Oracle should return upper case, but tried both anyway) and lower case column name ([Column(name: "CUSTOMER_ID")], [Column(name: "customer_id")]), etc.).

It looks like that Dapper does not support

System.ComponentModel.DataAnnotations.Schema.ColumnAttribute

Any help is highly appreciated.

1 Answer 1

1

Dapper has only edge support for this; however, Dapper.AOT (https://aot.dapperlib.dev/ - AOT extensions for Dapper by the same developer, with lots more configuration support) allows this option via either [DbValue(Name=...)] (Dapper.AOT's own attribute) orr [Column(...)] (the system attribute you already found).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.