Skip to content
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

Backing field type mismatch check is too aggressive #2058

Closed
rmja opened this issue Apr 24, 2015 · 1 comment
Closed

Backing field type mismatch check is too aggressive #2058

rmja opened this issue Apr 24, 2015 · 1 comment

Comments

@rmja
Copy link

rmja commented Apr 24, 2015

The check in MemberMapper.cs for type compatability between the backing field and the associated property it too aggressive. It is currently:

if (!fieldInfo.FieldType.GetTypeInfo().IsAssignableFrom(property.ClrType.GetTypeInfo()))
{
    throw new InvalidOperationException(
        Strings.BadBackingFieldType(fieldName, fieldInfo.FieldType.Name, entityType.Name, propertyName, property.ClrType.Name));
}

This does not allow for simple type conversion between the backing field and the domain property, which is useful when the store type does not exactly match the domain type.

public class ExampleEntity
{
    private int _foo; // Store type is int

    public ushort Foo { get { return (ushort)_foo; } } // Domain type is actually ushort, but the store does not support unsigned.
}

The backing field can in this case always hold the value of the property, and so it should also pass the check. Note that typeof(int).IsAssignableFrom(typeof(ushort)) is false.

@rowanmiller
Copy link
Contributor

Triage We have a work item to properly support type conversion (#242). What you probably want to do here is have everything in the CLR type defined as ushort and just have EF do the conversion. In the meantime, you could expose a private property typed as int and have EF map to that.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants