dbConnection.GetObject> Dapper Extensions

dapper dapper-extensions entity-framework mapping prefix

Question

Dapper Extensions dbConnection.Get issue (personId)

My model is named Person:

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

I have the following table in my database:

data.Persons
    Pers_Id
    Pers_Name

I see a "Invalid object name "Person"" problem when doing this without using any form of custom mapping. Because the problem only occurs when I fully map the model with the prefix "Pers_" and use "data.Persons," I think there is a mapping problem. Get to work

But is it possible to prefix a mapping automatically? The database I'm utilizing contains a wide variety of tables with various fields.

Additionally, I already have everything mapped to Entity framework. Is it possible for Entity DbModelBuilder to provide the map settings?

1
0
2/2/2017 8:08:10 AM

Popular Answer

Dapper-Extensions is based on convention. It uses.dbo for the schema and Id for the main key. You will need to build a specific mapping if the tables in your database don't follow the pattern.

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();
    }
}

Using Dapper and simply writing your inline queries is an alternative:

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

Update:

Another option is to experiment with T4 Text Templates and Code Generation.

Here's a meaningless example

2
2/6/2017 4:21:45 PM


Related Questions





Related

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