11

I'm using DapperExtensions v4.0.30319 and I'm trying to let Dapper.Contrib know that my schema is not DBO. I have provided:

public class EngineMapper : ClassMapper<Engine>
{
    public EngineMapper() : base()
    {
        Schema("vehicles");
    }
}

I understand from the DapperExtensions documentation (https://github.com/tmsmith/Dapper-Extensions/wiki/Customized-mapping-for-a-class) that this class will be automatically found using reflection?

But I also tried explicitly using:

DapperExtensions.DapperExtensions.DefaultMapper = typeof(EngineMapper);

Either way when I use Dapper.Contrib's:

SqlConnection.Insert(new Engine());

the resulting insert statement does not specify any schema.

How do I do an insert (or update, etc) using Dapper.Contrib where it uses the table schema which I specify?

Developer Webs
  • 983
  • 9
  • 29

2 Answers2

12

You can use Table attribute to show schema and table name together with a dot in between:

using Dapper.Contrib.Extensions;

[Table ("vehicles.YourTables")]
public class YourClass
{
    public int Id { get; set; }
    public string Name { get; set; }
}
vaheeds
  • 2,594
  • 4
  • 26
  • 36
  • 8
    I'll have to try this. But I really hate to pollute my (database-agnostic) models with information that really belongs at the database layer. – Developer Webs Apr 18 '17 at 15:41
  • 1
    Dapper.Contrib and Dapper Extensions are two separate libraries, so the Classmapper approach only applies to code using DapperExtensions. @vaheeds attributes approach is the right way to work with the Contrib library. – G Davison Apr 19 '17 at 14:20
0

I needed to also be able to map class to the custom schema. My project is asp.net core 2.2 Tried to decorate the poco with the [Table("schema.Table")] attribute, but dapper contrib won't seem to pick it up. Here are the proj. dependencies

enter image description here

However this approach did the trick:

enter image description here

See if that helps.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
mike123
  • 1,549
  • 15
  • 23