如何映射标识列,具有不同的名称,以小巧玲珑的? [英] How to map an identity column, that has a different name, with Dapper?

查看:122
本文介绍了如何映射标识列,具有不同的名称,以小巧玲珑的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ID列像BOOKID,AUTHORID,等我的代码文件的数据库,但是,只要有一个ID属性。我试图转换使用NHibernate的与小巧玲珑的程序的一部分,所以我试图消除这两个ID和一个BOOKID物业的需求。 NHibernate的有一个内置的身份映射,映射BOOKID到图书对象的Id属性,同样AUTHORID对作者的对象ID属性。

I have a database with id columns like BookId, AuthorId, etc. My code files, however, just have an Id property. I'm attempting to convert parts of the program that use NHibernate with Dapper, so I'm trying to eliminate the need for both an Id and a BookId property on . NHibernate has a built in identity map that maps BookId to the Id property of Book objects and similarly AuthorId to the Id property on Author objects.

有没有办法做到这一点短小精悍,给予一个别名为列,在SQL查询外?

Is there a way to do this Dapper, outside of giving an alias to the column, in the 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; }
}

这是我使用看起来像一个简单的查询:

A sample query that I'm using looks like:

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?

推荐答案

通过设计,hardy中不具有映射层。一步这条道路,我们知道发生了什么事,一下子我们将在配置文件中溺水之前。所以:两个选项:

By design, dapper does not have a mapping layer. One step down that road, and before we know what has happened all of a sudden we'll be drowning in configuration files. So: two options:


  • 添加别名在SQL

  • 在C#中添加一个垫片

所以要么(SQL):

select BookId as [Id], Name from Books

或(C#):

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

这篇关于如何映射标识列,具有不同的名称,以小巧玲珑的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆