Deleting an entity with a many-to-many relationship throws an ArgumentNullException
I have a
Memory
and Person
tables, with a junction table MemoryPerson
that represents a many-to-many relationship between them
The expected behaviour I want is to delete any related MemoryPerson
whenever a Memory
or Person
is deleted. I have repositories for each entity, and they all follow the same general implementation:
That said, here comes my issue: When deleting a Person
, all related MemoryPerson
get deleted as expected, however when doing the same for Memory
, an ArgumentNullException
is thrown with the message Value cannot be null. (Parameter 'key')
. The Call Stack points to that exact DeleteAsync
method.
I have checked and none of the values of the entity passed are null
, I also tried adding checks in each of the methods but to no success.
Does anyone have any idea of a possible solution? I'll send the OnConfiguring
override in the replies due to character limit.
A few extra details:
- The entities are created properly
- Deleting a memory without an existing MemoryPerson does not throw an exception
- Deleting a memory with a MemoryPerson that does not relate to said memory does not throw an exception27 Replies
It is currently configured this way:
(I apologize for broken indentation)
A simple recreation for the problem is just
Is this an issue with pasting? You're missing a ");" at the end below. Not sure if it will resolve your issue it's just the first thing that stepped out to me
modelBuilder.Entity<Memory>(entity =>
{
entity.HasKey(m => m.Id); // Primary key
entity.Property(m => m.Title).IsRequired().HasMaxLength(200);
entity.Property(m => m.Description).HasMaxLength(2000);
entity.Property(m => m.Date).HasColumnType("datetime");
// Relationship with MemoryPerson
entity.HasMany(m => m.MemoryPeople)
.WithOne(mp => mp.Memory)
.HasForeignKey(mp => mp.MemoryId)
.OnDelete(DeleteBehavior.Cascade);
}
yeah it was an issue with pasting, my bad
the
);
is present in the code, just was cut out when copying the source codeYour problem is how you're declaring your MemoryPerson, I think.
Try declaring it like this:
same error
Do you have a stack trace?
...well, one you can publically share, I guess.
i cant send full stack trace due to character limit but i can probably put on a pastebin if needed
Sure.
But even from that excerpt it seems like something didn't save correctly.
i thought about that but everything is being added to the database correctly
the memory, person and memoryperson are all saved in the database
I'm guessing the Memory object also has its Id populated by the time you try and delete it, right?
yep
And do you know for sure that your change tracker is tracking that entity when you try and delete it?
Or I guess a better question is "Is the DbContext you're using to delete the entity the same one you used to add it?"
it is, yes
Hmmm...
If you're using a repo pattern, I assume you have a Get method, right?
i do
Are you using FindAsync() or FirstOrDefault()?
FindAsync
Try getting getting the most up-to-date instance of that Memory object prior to deleting it.
If the change tracker is already tracking it, memory = await memoryRepo.GetAsync(memory.Id) shouldn't call the DB.
See if that also throws an error.
same exception
what bugs me is that whatever i do with
Memory
works perfectly with Person
, they call the "same" methods (from their respective tables), have pretty much the same implementation and only one of them has this issueMaybe show both of the repo classes for both entities?
wait first one is wrong
had some code that i was testing
i updated it to the proper one
Yeah, I'm stumped, unfortunately.
I'd have to dig deeper into your code and DB to troubleshoot this.
its alright, thanks for trying anyways :)
Memory type already exists in the standard library so you might want to rename it
That is not the issue here though, but I get what you mean
did you got it? @Gax
sadly no
at this point im considering rewriting it from scratch and see if a random change i didn't consider does it