首页 > 解决方案 > 如何使用 Dapper.SimpleCrud 设置模式名称?

问题描述

我正在使用 Dapper.SimpleCrud。有谁知道如何设置表的模式名称?我查看了文档,但没有找到与设置或更改架构名称相关的任何内容。

public class doc_info
{
    public int doc_info_id { get; set; }
    public int app_info_id { get; set; }
    public string doc_name { get; set; }
    public string file_loc { get; set; }
    public string doc_type { get; set; }
    public string doc_scope { get; set; }
    public int doc_order { get; set; }
}

标签: dapperdapper-simplecrud

解决方案


这是在 GitHub 上提出的旧请求:

一种解决方法是以这种方式在类上提供 TableAttribute:

[Table("Schema].[Table")]

该功能已包含在 GitHub 上,如下所述:

查看测试:https ://github.com/ericdc1/Dapper.SimpleCRUD/blob/master/Dapper.SimpleCRUDTests/Tests.cs#L83

[Table("CarLog", Schema = "Log")]
public class CarLog
{
    public int Id { get; set; }
    public string LogNotes { get; set; }
}

public void TestInsertIntoDifferentSchema()
{
    using (var connection = GetOpenConnection())
    {
        var id = connection.Insert(new CarLog { LogNotes = "blah blah blah" });
        id.IsEqualTo(1);
        connection.Delete<CarLog>(id);
     }
}

TableAttribute有一个属性Schema

[AttributeUsage(AttributeTargets.Class)]
public class TableAttribute : Attribute
{
    public TableAttribute(string tableName);

  //
    // Summary:
    //     Name of the table
    public string Name { get; }
    //
    // Summary:
    //     Name of the schema
    public string Schema { get; set; }
}

您应该在使用属性TableAttribute.Schema装饰 Entity/POCO 时设置此Table属性。用 .装饰你的 Entity/POCO 类[Table("YourTableName", Schema = "YourSchema")]


推荐阅读