11

I have the following issue with my update identifiers when seeding to my db:

context.ClientPromos.AddOrUpdate(
            cp => new { cp.ClientID, cp.Recommendation_ID, cp.PromoCode_ID },

            new ClientPromo
            {
                ClientID = 0,
                Recommendation_ID = Rec30Off.RecommendationID,
                PromoCode_ID = pc30PerOffProd.PromoCodeID
            },
            new ClientPromo
            {
                ClientID = 0,
                Recommendation_ID = RecKnow.RecommendationID,
            },

            new ClientPromo
            {
                ClientID = 0,
                Recommendation_ID = RecCall.RecommendationID,
            },
);

context.SaveChanges();

Since cp.Recommendation_ID and cp.PromoCode_ID are int? datatypes, it gets the following error:

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

I have viewed this article and added the modelBuilder - IsOptional() code described, but it doesn't work for me and I get the same error in this question.

If I change:

cp => new { cp.ClientID, cp.Recommendation_ID, cp.PromoCode_ID }

To:

cp => new { cp.ClientID }

It works fine, however this will not work if I need to update a record it will just duplicate every record in the table.

Community
  • 1
  • 1
Brian Bruce
  • 111
  • 1
  • 3
  • did you solve your issue ? Maybe that will help http://stackoverflow.com/questions/16818382/the-binary-operator-equal-is-not-defined-between-type-nullableint32-and-int32 – Lukasz S Jul 03 '15 at 15:39

2 Answers2

6

There is an error in Entity Framework AddOrUpdate method. I created an issue in EF repository:

https://github.com/aspnet/EntityFramework6/issues/9

MatthewT
  • 638
  • 6
  • 17
Radosław Maziarka
  • 655
  • 1
  • 6
  • 16
-1

I am not sure how much you're familiar with Nullable Types (C# Programming Guide) but the long story short as the name represents,

A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.

You can allocate a normal type to a nullable type but NOT the other way around as when the value is null we will get into trouble so compiler which catch this error in the compile time.

However, there are easier ways to go around this issue if that's your only one and a simple if statement will sort everything out before the allocation.

var myIntVar = myIntVar_Nullable ?? default(int);

which is a syntactic sugar for

var myIntVar = myIntVar_Nullable == null ? default(int) : myIntVar_Nullable;
Mehrad
  • 4,093
  • 4
  • 43
  • 61
  • But this only happens when i am placing the columns to be identified for the addorupdate function for the code first migration. Not sure how i can apply this to that. If i remove those columns it works fine, but by doing that it continues to add rows to the db instead of updating them. – Brian Bruce Jan 31 '15 at 04:13
  • I'll look into it in more specifics then – Mehrad Jan 31 '15 at 04:18