我可以為dap -dot-net映射指定DB列名嗎?

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


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?

dap -dot-net是否有一種方法可以使用屬性來指定應該使用的列名而不是屬性名?

public class Code
{
    public int Id { get; set; }
    public string Type { get; set; }
    // This is called code in the table.
    public string Value { get; set; }
    public string Description { get; set; }
}

I'd like to be able to name my properties whatever I choose. Our database has no consistent naming convention.

我想命名我的屬性,無論我選擇什么。我們的數據庫沒有一致的命名約定。

If not with dapper, are there any additional similar options?

如果沒有dapper,是否還有其他類似的選項?

4 个解决方案

#1


25  

You can also check out Dapper-Extensions.

您還可以檢查dap擴展。

Dapper Extensions is a small library that complements Dapper by adding basic CRUD operations (Get, Insert, Update, Delete) for your POCOs.

Dapper擴展是一個小型的庫,它通過為您的POCOs添加基本的CRUD操作(獲取、插入、更新、刪除)來補充Dapper。

It has an auto class mapper, where you can specify your custom field mappings. For example:

它有一個自動類映射器,您可以在其中指定自定義字段映射。例如:

public class CodeCustomMapper : ClassMapper<Code>
{
    public CodeCustomMapper()
    {
        base.Table("Codes");
        Map(f => f.Id).Key(KeyType.Identity);
        Map(f => f.Type).Column("Type");
        Map(f => f.Value).Column("Code");
        Map(f => f.Description).Column("Foo");
    }
}

Then you just do:

然后你就做的事:

using (SqlConnection cn = new SqlConnection(_connectionString))
{
    cn.Open();
    var code= new Code{ Type = "Foo", Value = "Bar" };
    int id = cn.Insert(code);
    cn.Close();
}

Keep in mind that you must keep your custom maps in the same assembly as your POCO classes. The library uses reflection to find custom maps and it only scans one assembly.

請記住,您必須將自定義映射保存在與POCO類相同的程序集中。庫使用反射來查找自定義映射,它只掃描一個程序集。

Update:

更新:

You can now use SetMappingAssemblies to register a list of assemblies to scan:

您現在可以使用setmappingassembly注冊要掃描的程序集列表:

DapperExtensions.SetMappingAssemblies(new[] { typeof(MyCustomClassMapper).Assembly });

#2


19  

If you are using a select statement directly or in a procedure you can just alias the columns.

如果直接使用select語句或在過程中使用select語句,則可以對列進行別名。

SELECT code as Value FROM yourTable

#3


3  

Another approach is to just manually map it with the dynamic result.

另一種方法是用動態結果手工映射它。

var codes = conn.Query<dynamic>(...sql and params here...)
                 .Select<dynamic,Code>(s=>new Code{Id = s.Id, Type = s.Type, Value = s.code, Description = s.Description});

Clearly this introduces type-safety scenarios because you are querying on dynamic. Also, you have to manually map columns which is a bummer.

很明顯,這引入了類型安全場景,因為您正在查詢動態。另外,您還需要手動地映射列,這是一個bummer。

However, I tend to like this approach because it's so darned transparent. You can cast if need be (as is the case with Enums), and basically just do whatever it is you need to do to go from the db recordset to your properties.

然而,我更喜歡這種方法,因為它是如此的透明。如果需要的話,您可以進行強制轉換(就像在枚舉中那樣),基本上只需執行從db記錄集到屬性所需要的任何操作。

#4


2  

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

對於select,可以向類中添加構造函數來執行映射。構造函數參數名稱必須與表列匹配。

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; }
    }

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/29/720bda6cef191f9d4ac33cb5d8146378.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com