Dapper Extensions dbConnection.Get dapper dapper-extensions entity-framework mapping prefix

Question

Problem with: Dapper Extensions dbConnection.Get(personId)

I have a model called Person:

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

In the database I have this table:

data.Persons
    Pers_Id
    Pers_Name

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?

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
        Table("table_name");

        //use a custom schema
        Schema("not_dbo_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
        AutoMap();
    }
}

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

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

Update:

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

Here is a trivial example



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why