使用Postgres数据库和适当的外壳(不使用实体框架)的c# POCO类

时间:2022-07-13 09:39:44

I have created a database using Postgres SQL and I want to interact with this database using the Npgsql ADO.Net provider library. All of my Postgres tables and columns have lowercase naming, but, I want my C# POCOS to have uppercase naming.

我使用Postgres SQL创建了一个数据库,我希望使用Npgsql ADO与这个数据库交互。净供应商库。我所有的Postgres表和列都具有小写命名,但是,我希望c# POCOS具有大写命名。

For instance, in Postgres my user table is named "users" (plural) and it has a user_name column. In C#, I want to map that table and column to a class called User (singular) with a property called UserName.

例如,在Postgres中,我的用户表被命名为“users”(复数),它有一个user_name列。在c#中,我想将这个表和列映射到一个名为User(单数)的类,并使用一个名为UserName的属性。

All of the examples I see for using data annotations to accomplish this are using an ORM like EF, but I don't want to use an ORM, just raw Npgsql. Will data annotations work if I'm just using Npgsql (raw) without an ORM? For instance:

我所看到的使用数据注释来实现这一点的所有示例都使用了ORM,比如EF,但是我不想使用ORM,只是原始的Npgsql。如果我只使用Npgsql (raw)而不使用ORM,那么数据注释是否有效呢?例如:

[Table("users")]
class User
{
  [Column("user_name")]
  [Required]
  public string UserName { get; set; }
}

2 个解决方案

#1


1  

Npgsql doesn't do any sort of mapping for you - it's just responsible for sending your SQL commands to PostgreSQL and providing you with the results. It's up to you to write whatever SQL you want - Npgsql is totally unaware of your POCOs or any annotations you have on them.

Npgsql没有为您做任何类型的映射——它只负责将SQL命令发送到PostgreSQL,并向您提供结果。您可以编写任何您想要的SQL—Npgsql完全不知道您的POCOs或它们上的任何注释。

If you don't want to hand-write SQL you need an O/RM such as Entity Framework or some other mapping layer. If you do decide to write your own SQL it's recommended you look at Dapper.

如果您不想手工编写SQL,您需要一个O/RM,例如实体框架或其他映射层。如果您决定编写自己的SQL,建议您查看Dapper。

#2


1  

Use QueryFirst (disclaimer: which I wrote) It will generate your pocos based on the schema returned from your query. The poco class name is based on the name you give the query, so you control the casing. To control the casing of the columns in the result set, use SQL aliases. (Result columns become properties in the generated POCO). No ORM, no dependencies, full control over your sql, all queries continually integration tested. All data access continually validated. What's not to like?

使用QueryFirst(我写的免责声明:)它将根据查询返回的模式生成pocos。poco类名基于您提供查询的名称,因此您可以控制框。要控制结果集中列的边框,请使用SQL别名。(结果列在生成的POCO中成为属性)。没有ORM,没有依赖,完全控制sql,所有查询持续集成测试。所有数据访问都经过验证。不喜欢什么?

#1


1  

Npgsql doesn't do any sort of mapping for you - it's just responsible for sending your SQL commands to PostgreSQL and providing you with the results. It's up to you to write whatever SQL you want - Npgsql is totally unaware of your POCOs or any annotations you have on them.

Npgsql没有为您做任何类型的映射——它只负责将SQL命令发送到PostgreSQL,并向您提供结果。您可以编写任何您想要的SQL—Npgsql完全不知道您的POCOs或它们上的任何注释。

If you don't want to hand-write SQL you need an O/RM such as Entity Framework or some other mapping layer. If you do decide to write your own SQL it's recommended you look at Dapper.

如果您不想手工编写SQL,您需要一个O/RM,例如实体框架或其他映射层。如果您决定编写自己的SQL,建议您查看Dapper。

#2


1  

Use QueryFirst (disclaimer: which I wrote) It will generate your pocos based on the schema returned from your query. The poco class name is based on the name you give the query, so you control the casing. To control the casing of the columns in the result set, use SQL aliases. (Result columns become properties in the generated POCO). No ORM, no dependencies, full control over your sql, all queries continually integration tested. All data access continually validated. What's not to like?

使用QueryFirst(我写的免责声明:)它将根据查询返回的模式生成pocos。poco类名基于您提供查询的名称,因此您可以控制框。要控制结果集中列的边框,请使用SQL别名。(结果列在生成的POCO中成为属性)。没有ORM,没有依赖,完全控制sql,所有查询持续集成测试。所有数据访问都经过验证。不喜欢什么?