Can I specify DB column names for dapper-dot-net mappings?

后端 未结 4 539
别跟我提以往
别跟我提以往 2021-02-05 05:04

Is there a way with dapper-dot-net to use an attribute to specify column names that should be used and not the property name?

public class Code
{
    public int          


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 06:02

    For selects, you can add constructors to your classes to perform the mapping. The constructor parameter names must match the table columns.

    Below is an example from the source. The table will be correctly mapped to the class.

    Table:

    CREATE TABLE #Users (Id int, Name varchar(20))
    

    Class:

       class UserWithConstructor
        {
            public UserWithConstructor(int id, string name)
            {
                Ident = id;
                FullName = name;
            }
            public int Ident { get; set; }
            public string FullName { get; set; }
        }
    

提交回复
热议问题