dotnet / dotnet/efcore

Support query translations that involve implicit conversions between types

Open
#32,030 3 comments 0 reactions 0 assignees View on GitHub
area-model-building area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Hey, i have the following:
```C#
// Strongly typed key
public record DbUserId(Guid Value)
{
public DbUserId() : this(Guid.NewGuid())
{
}

public static implicit operator Guid(DbUserId id) => id.Value;
public static implicit operator Guid?(DbUserId? id) => id != null ? id.Value : null;

public static bool operator ==(DbUserId id, Guid guid)
{
return id.Value == guid;
}

public static bool operator !=(DbUserId id, Guid guid)
{
return !(id == guid);
}

public static bool operator ==(DbUserId? id, Guid? guid)
{
if (id == null || guid == null) return false;
return id.Value == guid;
}

public static bool operator !=(DbUserId? id, Guid? guid)
{
return !(id == guid);
}
}

// entity
public class DbUser {
public DbUserId Id { get; set; }
// .. other relations
}

// Configuration
builder
.Property(x => x.Id)
.HasColumnName("Id")
.HasConversion(
x => x.Value,
a => new DbUserId(a)
);
```

The problem is, this works:

```C#
this._dbUser = await _db.Users
.Where(x => x.Id == new DbUserId(id))
.ToList();
```

But this code throws the exception
```C#
this._dbUser = await _db.Users
.Where(x => x.Id ==id)
.ToList();
```

`System.InvalidCastException: Object must implement IConvertible.
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.Sanitize[T](Object value)
at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass4_0`2.b__1(Object v)
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMapping.CreateParameter(DbCommand command, String name, Object value, Nullable`1 nullable, ParameterDirection direction)
at Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedRelationalParameter.AddDbParameter(DbCommand command, Object value)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Phoenix.API.Features.Users.UsersController.GetById(Guid id) in .........

`
image

macOS, .NET 7+, MS Sql server

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.