0

In EF6, I load my airport from the DB:

var existingAirport = db.Airports.SingleOrDefault(a => a.ID == airportID)
//airport is loaded, it has a countryID and virtual Country object

I perform some logic which makes some modifications to alter the relationship, changing both the country object and FK ID:

//it's a bit more complex, but simplified:
existingAirport.Country = //the someOtherCountryObject
existingAirport.CountyID = //the someOtherCountryObject.ID

I save the change (which should assign the airport the new country)

db.SaveChanges();

The problem is EF decides to null the relationship in both the DB and memory (existingAirport.Country and existingAirport.CountyID are both now null). The airport is now associated with neither the new or old country.

Can anyone think of a reason EF is doing this? The new FK points to a valid country.

FBryant87
  • 3,981
  • 2
  • 36
  • 63
  • 2
    What is the value of `the new country ID` at that point? – Gert Arnold Nov 16 '17 at 12:13
  • It is never required to assign both the object and the ID, why are you assigning both? Also, is `newCountry.Id == newCountryID`? – Camilo Terevinto Nov 16 '17 at 12:19
  • things to check on if you haven't already done, such as does your method pass the value and is this value a valid data type, is there any other values that are required and is not being entered or passed in your method – Jephren Naicker Nov 16 '17 at 12:31
  • @GertArnold The new country ID matches the ID of the country it's being changed to, I've edited to hopefully make this clearer (the IDs are all matching up correctly). – FBryant87 Nov 17 '17 at 13:03
  • @CamiloTerevinto If I set just existingAirport.CountryID (and leave existingAirport.Country as null) it also nulls the relationship, existingAirport.CountryID is nulled when SaveChanges() is called. – FBryant87 Nov 17 '17 at 13:04

2 Answers2

2

Looks like you are trying to add a new Country and I don't see that your new Country entity is added to the context.

Please refer to these posts to get an idea. Updating child objects in Entity Framework 6 and Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent

jbl
  • 123
  • 6
  • The new Country has been loaded (at least eagerly) from the DB with .Include() elsewhere - I can see it when debugging. Is this not necessarily enough to guarantee the context is aware of it? – FBryant87 Nov 17 '17 at 10:01
  • I've performed a check with "db.Set().Local.Any(c => c == existingAirport.Country)" and the entity is definitely in the context. EF still nulls the relationship. It also does it if I assign just the foreign key (ID) and not the virtual object. – FBryant87 Nov 17 '17 at 12:58
0

It took me a while, but this was caused by cyclic references in other parts of my model. For example I hold a list of Countries, and one of these Country objects (someOtherCountryObject in this case) had an EF-generated reference to the Airport.

I solved the problem by ignoring these references during the mapping stage, i.e. when mapping the Country to the DB:

.ForMember(c => c.AppName_Airport, opt => opt.Ignore())

This resolved the problem, preventing EF from nulling the relationship when SaveChanges() was called. My Aiport now has the country it should have, and no duplicates or any side effects have occurred.

FBryant87
  • 3,981
  • 2
  • 36
  • 63