4

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; }
}
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Weston Goodwin
  • 781
  • 3
  • 12

1 Answers1

4

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")].

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Don't know what I'm doing wrong. Neither [Table("[Zld_File_Data]", Schema = "[DI]")] nor [Table("Zld_File_Data", Schema = "DI")] nor [Table("DI].[Zld_File_Data")] worked. Workaround was to use [Table("[DI].[Zld_File_Data]")] (i.e. setting the Name property to a single string which included the schema. – Randy Kreisel Jan 04 '23 at 16:56