Dapper Rainbow - How to specify table name in another schema

1k views Asked by At

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

There are 4 answers

0
Acorax On

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;
     }
0
Maxolidean On

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...

0
gowk On

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...
0
O. Shai On

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]");
    }
}