Closed msanaei closed 9 years ago
Hi,
I can't tell really tell what's wrong after having a cursory glance over your description. If I had to speculate I'd say that one of the associated entities has navigation properties that are null after calling UpdateGraph
but the corresponding foreign key property is of a type that cannot be null (e.g. int). Could you please inspect the object that UpdateGraph
returns in a debugger and check if that's the case?
var inspectMe = _dbContext.UpdateGraph(..)
If that's not the problem please provide a minimal reproduction of your problem so I can debug it (for example in a gist).
Thank you @andypelzer , I uploaded a simplified version of my model, you can get it from https://www.dropbox.com/s/i9dvrb6ebd5wo7h/GraphdiffTest.rar?dl=0
Hi,
the "Sequence contains no element" exception in your sample is caused by mapping a collection as an associated entity:
After fixing that I also see the InvalidOperationException
you originally reported. It simply means that Entity Framework is complaining about navigation properties you've mapped as required but not set (they're null
). Have a look at the new AppUserInApplication
attached to the context:
Entity Framework requires you to not only to set ids but also the navigation properties. If you change your code to this everything works as expected:
Hope that helps!
As far as the first exception is concerned I've opened a separate issue (#100) to improve the error message.
Thank you @andypelzer, The error fixed but now when I run the program, it delete Role1
, Role2
and insert Role3
:
But I expect only Role3
add to other roles.
Hi,
yes, because that's what you're asking it to do. GraphDiff can't monitor what you do to the in-memory object graph, it can just compare the state of that graph (when calling UpdateGraph
) to the database. So if you provide it with a list containing just a single element it has to assume that's what you want to see in your database and it tries to recreate that state there. If you want to assign all three security roles instead, you have to add all those roles to appuserinapp.SecurityRoles
.
Thank you @andypelzer again, I asked this question in StackOverflow with bounty worth +100, too. If you want, you can post answer to my question ;)
Thanks, I have.
I'm using
EF code first 6.1
with.NET 4
in my application and have following classes in my model(I cut other unrelated parts of diagram. e.gPermission
has other navigations):My business logic works with detached entities so I'm using
RefactorThis.GraphDiff 2.0.1.0
to perform updates. I want to update anapplicationUserInApplication
object, so I get an existingapplicationUserInApplication
with itsSecurityRole
s from database and return it as aView-Model
then update it and map back it toapplicationUserInApplication
usingAutomapper
(in update operation, I only changeSecurityRoles
collection of anapplicationUserInApplication
, theseSecurityRole
s saved before and I only select them), so I defined following configuration:and defined following mapping for
AppUserInApplication
inAppUserInApplication_Mapping
class:After calling above
UpdateGraph()
, when I call_dbContext.SaveChange();
I get following error:[Updated]
I also, tried following mapping
But I get same error, yet.
Does anyone know where is the problem?