Quantcast
Channel: MySQL Forums - Connector/NET and C#, Mono, .Net
Viewing all articles
Browse latest Browse all 1451

Align GUID Representation For UUID_TO_BIN and EF Core (1 reply)

$
0
0
I am trying to understand and fix some odd behavior when using EF Core to retrieve items inserted via direct SQL.

## Version Info

- MySQL version: `8.0.25`
- .NET runtime version: `Core 3.1`
- EF Core version: `5.0.5`
- MySQL.EntityFrameworkCore version: `5.0.5`

## Description

When a UUID is added to the database via direct SQL, it does not match the format of an EF-inserted UUID such that it cannot be retrieved via EF, even when the string representation of the UUID is matches exactly. For example, if we add insert an item directly with the ID `UUD_TO_BIN('831136f0-aee0-47bc-b6ba-be04dc858990')` and then try to find the entity with ID `831136f0-aee0-47bc-b6ba-be04dc858990` EF will return NULL.

Note, this is without configuring EF in any particular manner--the default behavior is misaligned.

Furthermore, the `GuidFormat` connection string option is not supported to allow configuring this. As such there does not appear to be an easy way to fix this misalignment in representation. How can this be addressed or should we file a bug report?

## Example Code

```csharp
var id = Guid.NewGuid();
var idString = id.ToString();
dbContext.Database.ExecuteSqlInterpolated(@$"
INSERT INTO Entities (Id)
VALUES (UUID_TO_BIN({idString}))
");
dbContext.SaveChanges();

dbContext.Entities.Find(id).Should().NotBeNull(); // fails because UUID_TO_BIN represents `id` differently than EF
```

Viewing all articles
Browse latest Browse all 1451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>