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

AddOrUpdate error when identifyingProperty is nullable #9

Closed
rowanmiller opened this issue Jul 29, 2016 · 4 comments · Fixed by #67
Closed

AddOrUpdate error when identifyingProperty is nullable #9

rowanmiller opened this issue Jul 29, 2016 · 4 comments · Fixed by #67

Comments

@rowanmiller
Copy link
Contributor

Hi,

There is an error in AddOrUpdate method. Let's look on such example:

public class Test
{
    public Guid? SomeKey { get; set;}
}

protected override void Seed(TestContext context)
{
        var entity = new Test() { SomeKey = Guid.NewGuid()};
        context.Tests.AddOrUpdate(t => t.SomeKey, entity); 
}

Running such code creates error:

The binary operator Equal is not defined for the types 'System.Nullable`1[System.Guid]' and 'System.Guid'.

Problem lays in invalid condition createion based on identifier parameter in AddOrUpdate method:

Expression.Equal(
                            Expression.Property(parameter, pi.Single()),
                            Expression.Constant(pi.Last().GetValue(entity, null)))

Expression.Property returns expression of type Guid?. Expression.Constant return expression of type Guid. And Equals cannot join it.

The solution is to add parameter to Expression.Constant about type of property.

                            Expression.Constant(pi.Last().GetValue(entity, null), pi.Last().PropertyType))

Then it will cast this expression to be Guid? and it will run property.

I think that this will also occure on EF7. but i didn't use it already.

@rowanmiller
Copy link
Contributor Author

Ported from CodePlex https://entityframework.codeplex.com/workitem/2907

@rowanmiller rowanmiller added this to the 6.2.0 milestone Jul 29, 2016
@Daniel-Svensson
Copy link

Great that you are working on this, hopefully it should also solve https://entityframework.codeplex.com/workitem/2486 which might be a duplicate of the other codeplex item

@axelheer
Copy link
Contributor

Yeah, just adding the property's type to the constant expression should do it (see my corresponding Stack Overflow answer).

I could find some time for this next week or the week after. Please let me know, if I should look into it and prepare a PR if successfully.

@redwyre
Copy link

redwyre commented May 17, 2017

Is there a work around for this? I tried copy this code into my project, but it uses internal types.
Edit: I used a combo of http://stackoverflow.com/questions/15405875/ef5-migrations-seed-addorupdate-with-a-nullable-selection-criteria/29339351#29339351 modified with http://stackoverflow.com/questions/17710769/can-you-get-the-dbcontext-from-a-dbset/17712575#17712575 to make it a drop-in replacement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants