dapperdapper-simplecrud

How to set Schema name using Dapper.SimpleCrud?


I am using Dapper.SimpleCrud. Does anyone know how to set the schema name for a table? I have looked through the documentation but I have not found anything relating to setting or changing the schema name.

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

Solution

  • This was old request raised here on GitHub:

    A work around is to provide a TableAttribute on the class in this fashion:

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

    The feature was included as stated here on GitHub:

    See the tests: 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);
         }
    }
    

    Class TableAttribute have a property 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; }
    }
    

    You should set this TableAttribute.Schema property while decorating Entity/POCO with Table attribute. Decorate your Entity/POCO class with [Table("YourTableName", Schema = "YourSchema")].