Skip to content

Allow writing enums as integral types #2435

Closed
Closed

Description

@roji

Allow CLR enums to be written to the database as their underlying integral types (EF6 depends on this, see npgsql/EntityFramework6.Npgsql#105).

First, if possible, it seems desirable to do this in the integral handlers (e.g. Int32Handler) rather than adding general logic to type handler (i.e. by overriding ValidateObjectAndGetLength, ValidateAndGetLength and friends). However, doing this efficiently could be tricky - it's out of the question to add upfront checks if something is an enum - this would slow down int writing for everyone. A post-failure check would work well, but unfortunately all the validation/writing methods throw rather than return failure, and catching that exceptions is bad because it implements flow control via exceptions.

If this is implemented as general logic in NpgsqlSimpleTypeHandler things are a bit simpler, but the code generators for non-generic parameters would need to be modified as well to perform the enum logic, which isn't going to be pretty. It would have been ideal if the validation/writing methods simply didn't throw, making them more composable.

In any case, this needs to work both for non-generic and generic parameters (i.e. NpgsqlParameter).

/cc @rwasef1830

Activity

  1. added this to the Backlog milestone on Apr 16, 2019
  2. roji commented on Apr 16, 2019

    @roji
    MemberAuthor

    Thinking about this again, it should be possible to stick all logic on Int32Handler (and the other two), without calling the base implementations - simply by switching on the type and dispatching into the right method. Then the enum-specific logic can be added.

    However, this would probably mean that most of NpgsqlSimpleTypeHandler.WriteWithLengthInternal<TAny> would need to be duplicated into the three integral handlers...

  3. YohDeadfall commented on Apr 16, 2019

    @YohDeadfall
    Contributor

    Is there any way to implement this in EF6 itself? Maybe via a value converter if it exists? I prefer to move it off the driver. Otherwise, enumerations support would be weird.

  4. rwasef1830 commented on Apr 16, 2019

    @rwasef1830
    Contributor

    I have an idea of using an EF6 interceptor to convert the values to actual ints or shorts or longs before they're sent to Npgsql. This way the type check would pass and Npgsql doesn't need to be aware of the hack. I'll experiment with it.

    I have previously written an example of an interceptor that forces all date times and date time offsets to be Utc. npgsql/EntityFramework6.Npgsql#66

    EDIT: Yep, my idea works. I'll see how to get this into the EF6 driver.

  5. rwasef1830 commented on Apr 16, 2019

    @rwasef1830
    Contributor

    This is no longer needed. A workaround was added in the EF6 driver

  6. roji commented on Apr 16, 2019

    @roji
    MemberAuthor

    Great, definitely better to not have to do this.

  7. usmanniqbal commented on May 8, 2019

    @usmanniqbal

    @rwasef1830 I am also facing this issue after migrating from 3.2 to 4.0, what is the workaround you are referring to?

    And is it going to help with TSQL executing from NpgsqlConnection in which EF6 is not involved?

  8. rwasef1830 commented on May 8, 2019

    @rwasef1830
    Contributor

    A workaround was committed to the EF6 driver, but it's waiting for a release, until a release is made you can attach an interceptor. See: npgsql/EntityFramework6.Npgsql#105 (comment)

  9. roji commented on May 9, 2019

    @roji
    MemberAuthor

    Sorry the release has been taking so long. There are several releases upcoming (Npgsql, Npgsql EF Core) so I'll try hard to also do on for the EF6 provider.

  10. damiarnold commented on Feb 13, 2020

    @damiarnold

    Any updates on this? I'm running into this issue upgrading a collection of projects that had Npgsql 3.2.x to 4.x, and I may have to revert back 3.2.

    While there are some cases in these projects where ORMs are involved (NHibernate, for example) that already have type handlers converting to/from Int32 before the underlying ADO.NET driver is called, there are many locations in these same projects where "raw" ADO.NET calls pass Enum types as parameters where the System.Data.DbType is explicitly set as Int32, but it fails with the "Can't write CLR type ..... with handler type Int32Hander".

    As these projects are somewhat database agnostic, the generic System.Data.Common.* structures are used, and have worked for several years until this point (using Npgsql <= 3.2, SqlClient, etc. as the underlying providers) it would be nice if this were fixed. If necessary, I may be able to spend some time looking into this and submit a patch, since it seems some of the problem has already been identified in this thread.

  11. roji commented on Feb 13, 2020

    @roji
    MemberAuthor

    @damiarnold which specific project (ORM) are you using that lacks this capability?

  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions