7

I am using Dapper Extensions to do some simple CRUD operations on a DB. My problem is that the tables I am using are held in a different schema to dbo. Is there a way to choose the schema at the dapper extensions level?

or

Should this be dealt with via the user that is being used to connect to the db with?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Mathew Padley
  • 102
  • 1
  • 8

1 Answers1

8

You can use the AutoClassMapper to assign a new schema to your model. An overview on this is on the extensions site. You will basically need to create an AutoClassMapper per model with a different schema. The best place to declare it is alongside your model itself like:

public class MyModel 
{
  public Guid Id { get; set; } 
}

public class MyModelMapper : AutoClassMapper<MyModel>
{
  public MyModelMapper() : base()
  {
    Schema("YourNewSchema");
  }
}
Wolfwyrd
  • 15,716
  • 5
  • 47
  • 67
  • How do we register MyModelMapper, or use it, so that Dapper will use the new schema specified by the example? I've followed your example, but Dapper still does inserts using no schema specified. Granted I'm doing inserts with Dapper.Contrib, perhaps DapperExtensions doesn't work with Dapper.Contrib? I used: DapperExtensions.DapperExtensions.DefaultMapper = typeof(MyNewMapper); It compiles, it just doesn't use the schema specified in MyNewMapper. – Developer Webs Mar 20 '17 at 13:10
  • This works perfect, thanks! Also Table("TableName") <-- is useful – Michael K May 02 '17 at 20:18
  • re: "How do we register MyModelMapper" : "By default Dapper-Extensions will scan the assembly containing it's assembly for any custom class mappings." ( https://github.com/tmsmith/Dapper-Extensions/wiki/Customized-mapping-for-a-class ) – jrr Jun 12 '18 at 16:30