✅ EF Core doesn't stop tracking list items.
Trying to edit an office entity and this works great for everything, except when the editedOffice passes less salesPerson ID's then there were before. If there are less id's it should remove them from the list inside the db aswell, currently it's not doing that. It is however adding new ones correctly when you pass more ids that werent there before.
31 Replies
In office:
When debugging I can see it gets replaced correctly in
office
but savechangesasync doesnt remove the item from the list?
I even tried to set them to null and that doesn't work?
Are you re-using the same DB context for all of these operations?
Yes I am
dbContext
And what type of application is this? API? some kind of native app?
Are these modifications all done in one request or is this something that could be spaced out over some time?
Because in general you're supposed to treat a
DbContext
as a single unit of work. Basically reuse the same context for the lifetime of a request, then throw it away. They're not meant to be singletons that are reused over the lifetime of an application.ASP .NET CORE Controller Rest API
Yes this is one request the specified code above is the service layer
Except the office mentioned that is inside the domain layer (entity class)
ok, wanted to make sure it wasn't anything getting wonky with singleton dbcontext or something
Oh no don't worry the dbContext is done properly via DI
I think I've found something
I just tried to seed a salesperson without an office
it might just be my migrations not allowing null...
But I don't get why its not throwing an error there then
what does your
SalesPerson
entity look like?ya you probably want
OfficeId
to be an int?
so EF realizes that it's nullableGotcha its not enough to set Office nullable? I thought ef would infer that 😅
well, you don't have to load nav properties
so a nav property being nullable doesn't necessarily mean the relationship is nullable
and since you've specified
OfficeId
I'm going to assume EF sees that, sees it's a non-nullable FK, and uses that to build the relationship
but you not getting an exception makes me think EF isn't detecting the removal or isn't properly tracking values
i never write my entities with IReadOnlyCollection<>
and backing fields so I'm not sure if that could be causing issuesOkay that seems to have worked
:Blob:
Its now removing it correctly
Yeah that's one odd thing
Its probably because of the IReadOnlyCollection no?
it could be, but I've never used backing fields for nav properties
I used it so I couldn't insert doubles
How do you normally set this constraint?
well you can prevent double inserts by configuring unique indexes too
Like
OfficeId
in this case you mean or?i assume sales person can be assigned to a single office?
(or no office)
Yes one to many relationship, not required relationship
what kind of potential double inserts do you think you could get then? I'm assuming this part of the API does not handle creating salespersons but only sends existing ones
worst case scenario, you try adding a user to the same office twice and you likely get an exception
but I usually keep all logic out of my entities and do any validation / guarding against stuff like this in the service layer
I see yes in this case its true. The
ReadOnlyCollection
is not necessary here.
It was actually a lecturer of mine who suggested this readonly
list (for another entity class but I also implemented it here)m but it has no use case other than avoiding thrown Exceptions.makes sense
in the case where a double insert could be possible (say a many-many relationship and you've explicitly defined a join table), you can always add a unique index that uses both FKs
you'd do something like
Gotcha thanks, I appreciate the help really much.
Just one more question, it may be a nooby question, what do you mean here when referring to a
backing field for nav properties
is that the Id or?
herei was referring to this
Oh you meant I also could do it in the modelbuilder without defining it as a nav property?
nah i was just saying that I've never used backing fields for a nav prop
i usually do
public List<SalesPerson> SalesPeople { get; set; }
but that is mostly personal preference, I don't like putting any business logic in my entitiesOoh gotcha I totally understand now 🙂
Thanks for explaining and taking time to help me
I really appreciate it!
np :PepoSalute:
❤️