Dapper Rainbow - How to specify table name in another schema
Asked Answered
G

4

6

I am pretty new to Dapper Rainbow so I may be missing something obvious. Is it possible to specify the table name and if so how?

I have tried the following with no luck.

public class DashboardContext : Database<DashboardContext>
{
    public DashboardContext()
    {
       this.DashboardResults = new Table<DashboardResult>(this, "Monitor.DashboardResult");
    }

    public Table<DashboardResult> DashboardResults { get; set; }
}
Geometrician answered 14/9, 2012 at 11:57 Comment(0)
D
1

I had the same problem but it seems an error in the code. I´ve just commented the lines where is setting the constructor for tables (Database.cs) and it works.

    internal void InitDatabase(DbConnection connection, int commandTimeout)
    {
        this.connection = connection;
        //this.commandTimeout = commandTimeout;
        //if (tableConstructor == null)
        //{
        //    tableConstructor = CreateTableConstructorForTable();
        //}

        //tableConstructor(this as TDatabase);
    }

I guess this is not the best solution...

Duntson answered 17/8, 2013 at 0:41 Comment(0)
A
0

You need to hack the rainbow source to get it to work. Find the CreateTableConstructor method in the file of DataBase.cs. Just add some code as following:

...
var setters = GetType().GetProperties()
    .Where(p => p.GetValue(this, null) == null 
             && p.PropertyType.IsGenericType 
             && p.PropertyType.GetGenericTypeDefinition() == tableType)
    .Select...
Articulate answered 8/2, 2015 at 8:45 Comment(0)
L
0

For anyone else stumpeling over this post like I did this is now fixed in Dapper.Rainbow version 0.1.3.

It is still in beta at this time (0.1.3-beta1) so if you want to use schema you can clone/fork the repository and run the build script. The binary output can then be used directly or packaged.

As for the table setup you need to define the table name with the name of the schema for for that particular table so for example looking at this example without schema

    public class MyDatabase : Database<MyDatabase>
    {
        public Table<Order> Order{ get; set; }
        public Table<Customer> Customer { get; set; }
        public Table<Item> Item { get; set; }
    }

Which works if you are only using dbo. but if you are for instance using say Product schema for Item you would have to define it using a constructor

    public class MyDatabase : Database<MyDatabase>
    {
        public Table<Order> Order{ get; set; }
        public Table<Customer> Customer{ get; set; }
        public Table<Item> Item;

        public MyDatabase()
        {
            Item = new Table<Item>(this, "Product.Item");
        }
    }

The rest should be as before

     using (var connection = DbConnections.Create())
     {
            connection.Open();
            var db = MyDatabase.Init((DbConnection)connection, commandTimeout: 2);
            var insert = db.Customer.Insert(
            // .
            //..... your object
            // .
            );
            var insertId = insert.Value;
     }
Leonard answered 28/12, 2016 at 11:45 Comment(0)
W
0

Based on @Acorax answer, it wasn't enough for me, I needed to add brackets to the schema and table name to solve this.

So this solved me the schema problem:

public class MyDatabase : Database<MyDatabase>
{
    public Table<Item> Items;

    public HamenasDbSchema()
    {
        Items = new Table<User>(this, "[Schema].[Items]");
    }
}
Welldressed answered 29/1, 2022 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.