简体   繁体   English

使用Dapper.NET将2个SQL表列值添加到一个C#属性

[英]Adding 2 sql table columns value to one C# property, using Dapper.NET

I have a class Department . 我有一个类Department

Deparment has one property called Address . Deparment拥有一个称为Address属性。

I have a SQL table called Deparment . 我有一个称为Deparment的SQL表。

This Department has 2 columns Address1 and Address2 . Department有2列Address1Address2

I use Dapper.net and would like to put the two SQL table columns into the one property of my class. 我使用Dapper.net,并希望将两个SQL表列放入类的一个属性中。

How ? 怎么样 ? Thank! 谢谢!

I would do: 我会做:

public class Department {
    public string Address1 {get;set;}
    public string Address2 {get;set;}

    public string Address {
        get { return Address1 + Environment.NewLine + Address2; }
    }
}

Are you using this for "Read-Only". 您是否将此用于“只读”。 How would you expect your ORM to write the data back to the database ? 您希望ORM如何将数据写回到数据库中?

If it is read-only, instead of querying the table directly, you could wrap it in a view. 如果它是只读的,则可以将其包装在视图中,而不是直接查询该表。

CREATE VIEW DepartmentView
(
    SELECT Address1 + ', ' + Address2 as Address FROM Department
)

and then just map the view instead. 然后只需映射视图即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM