简体   繁体   English

如何在 postgres 中插入 C# object

[英]How do I insert a C# object in postgres

I have a legacy code which I'm trying to change.我有一个我正在尝试更改的遗留代码。 This legacy code stores and retrieves data from local storage.此遗留代码从本地存储中存储和检索数据。 I'm trying to change it to save data, specifically c# classes, in a postgres database.我正在尝试更改它以在 postgres 数据库中保存数据,特别是 c# 类。 The way the legacy code stores data is this:遗留代码存储数据的方式是这样的:

FileSerializer.Serialize<T>(filename, instance);

I know how to INSERT INTO a serializable java object in postgres but I can't find a tutorial on how to do it with a c# object. I know how to INSERT INTO a serializable java object in postgres but I can't find a tutorial on how to do it with a c# object.

Thanks for your time!谢谢你的时间!

I ended up using Dapper as it supports ORM and postgres.我最终使用了Dapper ,因为它支持 ORM 和 postgres。

It's simple with NEntityDb .使用NEntityDb很简单。 You can find samples of how to insert objects here .您可以在此处找到有关如何插入对象的示例。

using (DbInstance dbInstance = new DbInstance()) 
{    
    int rowsAffected = dbInstance.NonQueryOf<Customer>(new Customer
    {
        FirstName = "Clair",
        LastName = "Guiet",
        TaxCode = "3720045188",
        Email = "cguiet@gov.uk",
        BirthDate = new DateTime(1980, 4, 15),
        Points = 10
    })
    .Insert()
    .RowsAffected;
}

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

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