How to map identity columns that have a different name with Dapper?

I have a database with id columns like BookId, AuthorId etc. My code files, however, have the Id property. I am trying to convert parts of a program that use NHibernate with Dapper, so I am trying to eliminate the need for both the Id and the BookId property. NHibernate has a built-in ID card that maps the BookId to the Id property of Book objects and is similar to the AuthorId for the Id property in Author objects.

Is there any way to make this Dapper outside of providing an alias to a column in a sql query?

public class Book { public int Id { get; set; } public string Name { get; set; } } public class Author { public int Id { get; set; } public string Name { get; set; } } 

An example request that I use looks like this:

 select * from Books b inner join Author a on a.AuthorId = b.AuthorId 

If Dapper doesn't support this easily, any thoughts on what other options I have?

+6
source share
1 answer

By design, dapper does not have a display layer. One step down this road, and before we find out what happened all of a sudden, we will drown in configuration files. So: two options:

  • add alias to SQL
  • add padding in c #

so (sql):

 select BookId as [Id], Name from Books 

or with#):

 private int BookId { get { return Id; } set { Id = value; } } // just for dapper 
+15
source

Source: https://habr.com/ru/post/909319/


All Articles