Closed ensemblebd closed 3 years ago
The code you show above should work fine without any modification to the EF templates.
From what I see, Customer
has an Id
type of int
. What is the identifier type for Address
?
Same for Address, both are type int
for this sample.
After deeper consideration, I believe the true problem was caused by using many-to-many
and writing a query loop with an "improper exchange" of sorts.
ie.
table A
, and looping over it's table B
records via nav prop.
table B
records, by using that many-to-many
navigation property loop
(ie. thus populating the internal dbcontext change tracker)ctx.SaveChanges()
table B
records and modifying them directly, without nav prop usage.
ctx.SaveChanges()
Regardless of if the table has an index, as improperly presumed as a fix in my original post, the problem occurs when manipulating entities twice in the same context, using new instances of the class, via different methods of querying.
100% a user error, and 100% a DbContext change tracker usage issue. Can definitely close this or delete this thread, and thank you as always for all you do. You have an incredible product here that we all benefit from greatly.
Checking if anyone else has come across this issue, as per:
For me the fix was to change
EFCore5ModelGenerator.ttinclude
: Line # 322:and then Line # 571:
Wanted to get your expert opinion on this, since I'm fairly new to the many-to-many use case and really just trying it out for first time. After the above change, I was able to run
ctx.SaveChanges()
successfully. Note that the first time you call it, it works fine. But the second time you call it it fails.Such as (pseudo code sample):
Not sure if this is necessary, since it's only affecting me. Which makes me wonder what I'm doing wrong