Skip to content

Add C# support #373

Description

@eikster-dk

First of all, I'm a huge fan of sqlc and I use it every single personal go project where I need database support. Good work 🙏

I noticed that you have started adding support for other languages incl. typescript and kotlin. In my day to day work I write a lot of code in C# which use postgres database.

Would you consider adding support for C# based apps?

I might be able to help but I'm still learning Go so I might need some support in how to get started if you want help with this

Activity

  1. kyleconroy commented on Mar 9, 2020

    @kyleconroy
    Collaborator

    Would you consider adding support for C# based apps?

    I would. The main issue that I don't know what good C# support would look like. Kotlin support was added by a contributor, so I see no reason by C# couldn't be added the same way.

    The first step would be to gather some more information. What does your current setup look like? What code are you writing by hand? What libraries are you using?

  2. eikster-dk commented on Mar 14, 2020

    @eikster-dk
    Author

    Currently the following libraries are used when I work with postgres and c#:

    My setup is to hand write the C# classes and the sql, then dapper (micro orm) providing the mapping from sql to my C# POCO class with the help of reflection. The thing is that I need to manually keep track that my queries and the c# class needs to match - this can be a bit error prone at times.

    Let me try and come up with an example of how the auto generated code could look like in the next couple of days.

  3. kamil-mrzyglod commented on Aug 12, 2022

    @kamil-mrzyglod

    Hey,

    Just recently found that library and it looks really cool, especially when its use is considered for projects already managing DB entities with rather low-level ORMs like aforementioned Dapper.

    @kyleconroy - is C# support is still something you'd still actively consider? There're in fact three main approaches when building an app in C# with DB communication in mind:

    • pure ADO.NET
    • Dapper as lightweight overlay for ADO.NET
    • Entity Framework which is in fact much heavier overlay for ADO.NET allowing for querying a DB with C# code instead of SQL queries

    Dapper is a DB-agnostic ORM meaning it supports many (if not all) .NET ADO providers being an interesting safe choice. Pure ADO.NET may be closer to your original approach for sqlc, but is not a first choice for most of the projects (in terms of popularity Entity Framework is probably something used for most of business projects with complex domains).

    I'd say the best option would be to get started with Dapper (as it abstracts many lower-level concepts from ADO.NET) and give it a try as the simplest option.

  4. d47id commented on Sep 14, 2022

    @d47id

    Personally I think pure ADO.NET with Npgsql is the right way to go. Dapper, IMO, adds an unnecessary layer of reflection-based object field mapping.

    The point of sqlc is to generate simple, performant, type-safe code to access queries and query results. It doesn't really matter if that code ends up being verbose compared to what you can get with Dapper.

    I also think there's a strong argument to be made for avoiding another dependency.

  5. Jlll1 commented on Sep 16, 2022

    @Jlll1

    C# support would also mean the need for supporting SQL Server, since that's what the majority of C# projects are running.

  6. kyleconroy commented on Nov 9, 2022

    @kyleconroy
    Collaborator

    Codegen plugins are now stable enough. Both Python and Kotlin support has been migrated to plugins. This means that if someone would like to try their hand at C# support, you aren't blocked getting your changes into the sqlc codebase.

  7. added and removed
    futureIn the year 3000...
    on Nov 9, 2022
  8. nickpreston24 commented on May 2, 2023

    @nickpreston24

    Hi there.

    The easy way to get C# support is this magic library. https://github.com/jonwagner/Insight.Database

    I'm trialing it and it's super-easy to set up C# DTOs and interfaces and run existing stored procedures or inline SQL.

    It's like Dapper, but on steriods. No EF Context 💩. No need to fill in the gaps for ADO.NET. Just pure bliss as advertised (at least, so far that's been my experience).

  9. SockworkOrange commented on Feb 15, 2024

    @SockworkOrange
    Contributor

    @doron050 and myself are starting work on C# support in here :)

  10. doron050 commented on May 18, 2024

    @doron050
    Contributor

    For anyone interested in SQLC for C#
    @SockworkOrange and I have been developing a plugin for C# as said above.
    Now we have a stable mature 1st version (v0.10.0) with support for MySQL and Postgres as well as all available .NET target frameworks, take a crack at it and we'd love feedback if you have any issues, PRs, just go for it 🎉

    TLDR

    version: "2"
    plugins:
    - name: csharp
      wasm:
        url: https://github.com/DionyOSS/sqlc-gen-csharp/releases/download/v0.10.0/sqlc-gen-csharp_0.10.0.wasm
        sha256: 613ae249a541ab95c97b362bd1b0b572970edcad5eb2a11806a52d3f95e0f65f
    sql:
      # PostgreSQL Example
      - schema: "examples/authors/postgresql/schema.sql"
        queries: "examples/authors/postgresql/query.sql"
        engine: "postgresql"
        codegen:
          - plugin: csharp
            out: NpgsqlExample
            options:
              driver: Npgsql
              targetFramework: net8.0
              generateCsproj: true
              filePerQuery: false
      # MySQL Example
      - schema: "examples/authors/mysql/schema.sql"
        queries: "examples/authors/mysql/query.sql"
        engine: "mysql"
        codegen:
          - plugin: csharp
            out: MySqlConnectorExample
            options:
              driver: MySqlConnector

    For further information refer to https://github.com/DaredevilOSS/sqlc-gen-csharp

  11. SockworkOrange commented on Jul 19, 2024

    @SockworkOrange
    Contributor

    @kyleconroy can be closed then:)

  12. doron050 commented on Dec 27, 2024

    @doron050
    Contributor

    Hi, @kyleconroy, just a reminder, can be closed :)

  13. kyleconroy commented on Mar 24, 2025

    @kyleconroy
    Collaborator

    I was waiting for everything to be stable. Sounds like that just happened this month. Closing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions