dbConnection.GetObject> Dapper Extensions

dapper dapper-extensions entity-framework mapping prefix


Problem with: Dapper Extensions dbConnection.Get(personId)

I have a model called Person:

    public int PersonId { get; set; }
    public string Name { get; set; }

In the database I have this table:


When I try this without any kind of custom mapping, I get an error "Invalid object name 'Person'." I believe this is a mapping issue, because when I completely map the model with the prefix 'Pers_', and use 'data.Persons' Get works.

But is there a way to automatically map with a prefix? The database I'm using has many different tables with different prefices.

I also have everything already mapped to Entity framework. Is there a possibility of getting the map settings from Entity DbModelBuilder?

2/2/2017 8:08:10 AM

Popular Answer

Dapper-Extensions is convention based. For schema, it uses .dbo and for primary key it uses Id. If your tables don't match the convention, you will have to create a custom mapping.

public class MyModelMapper : ClassMapper<MyModel>
    public MyModelMapper()

        //use different table name

        //use a custom schema

        //have a custom primary key
        Map(x => x.ThePrimaryKey).Key(KeyType.Assigned);

        //Use a different name property from database column
        Map(x=> x.Foo).Column("Bar");

        //Ignore this property entirely
        Map(x=> x.SecretDataMan).Ignore();

        //optional, map all other columns

An alternative is to use Dapper and just write your inline queries:

connection.Query("select * from foo.table where myId = {myId}", new {myId})


Another alternative is to play around with Code Generation and T4 Text Templates

Here is a trivial example

2/6/2017 4:21:45 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow