-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Int Enum with Flags can't read from database if more than one flag is set #814
Comments
Just tested it with using nvarchar instead if int and that works, so it seems to be a problem with Flags and Enums as int. |
Something weird is going on I think, if I just change the database column from int to nvarchar then i works without doing anything else, |
Interesting. We will investigate and get back to you in relation to this. Is this a blocker on your side? Or, can you utilize the NVARCHAR type for now? |
No blocker, the NVARCHAR works for now :) and nothing that is going to production anytime soon. |
@mikependon I might have to bump this to a bit more critical, we are now having problems with BulkInsertAsync and this. public async Task UpdateContractOwner(int customerNumber, ContractOwner contractOwner)
{
using var connection = new SqlConnection(ConnectionString);
await connection.ExecuteNonQueryAsync(@"
UPDATE dbo.Customer SET
ContractOwner = @contractOwner
WHERE CustomerNumber = @customerNumber"
, new { customerNumber, contractOwner });
} This inserts an int even though it is an NVARCHAR in the database, which is what we want. I guess this works is because there is no tabel mapping going on. But we also have a migration stage that is using BulkInsertAsync with Entity mapping, this is now inserting the names of the flags rather than the int value and we are getting a crash since we only did NVARCHAR(2). We can ofc increase the NVARCHAR to a value that can fit all combinations but we really want to use int. Is there some way we can force the BulkInsert to use Int event though the database is using NVARCHAR? The |
By default, the library is using the string types when pushing the data into the database, specifically if it uses the Note: You do not need to define the database type if you are using the model-based methods. In your case, we cannot do any further for
But since you mentioned that when you call the |
Oops, wait, I can replicate your problem if the enum is marked as |
I think, the ideal fix for this should save the string representation of the bit-wise enum (i.e: "Apple | Banana | Orange") if the underlying column is of NVARCHAR type, otherwise, it will save the INT type. This is now true to the normal enumeration, which is not a bit-wise. |
Yes that is how I would like for it to work as well, but I think it needs to respect the DbType set by Fluent, Attribut, Default etc as well. |
Just ran into this as well. Given the following enum, and an [Flags]
enum ExampleFlagEnum
{
A = 1 << 0,
B = 1 << 1,
C = 1 << 2
} Throws an error:
I've narrowed that down to the following code: RepoDB/RepoDb.Core/RepoDb/Reflection/Compiler/Compiler.cs Lines 641 to 655 in 744a543
Which generates the following expression: What's the reason for going from a database |
Bug Description
I'm using int Enums with the Flags attribute and inserting with ExecuteNonQueryAsync and AnonymouseType is working but when I try and read a row that have more than one flag set I get the following crash.
It might be connected to #624.
This is how i do my setup:
And call to database:
Exception Message:
Schema and Model:
Please share to us the schema of the table (not actual) that could help us replicate the issue if necessary.
And also the model that corresponds the schema.
Library Version:
Example: RepoDb v1.12.7 and RepoDb.SqlServer v1.1.3
The text was updated successfully, but these errors were encountered: