Hello, all.
So I’ve been working on this really large project for months.
It has two blazor websites(one for the public, one for authenticated admins and mods). those parts aren’t relevant
It has a NET Core 3.1 Web API. This one seems to be the issue.
It uses SqlServer Express.
When unit testing the repo and controllers, they all work just fine, but once I get to wori=king with the real database via SQL Server, I have a lot of issues and my integration tests show that it’s always SQL Server refusing certain operation.
-Any test that involves a single entity works just fine. I can Update, Create and Delete all I want.
– But as soon as more than one entity is involved, it refuses to work
EXAMPLE: I try to test wiping a PostCategory(custom class) which a Post relies on. The expected result is that instead of wiping, the description of the category will explain that it was supposed to be deleted, but was not because a Post relies on it;
I create the new Category- works
I create a new Post that relies on that PostCategory – fails, because of the foreign key that points to the previously created PostCategory. For some reason it won’t awknowledge its existance.
I try to wipe the Category and it gets completely wiped, becuse obviously step 2 failed and it has no Posts that rely on it.
Suggestion – replace #1 and #2 with :
I create the new Category- works
Query db for the existence of the category.
This is basic debugging, to see if the code at #2 has a fundamental flaw (e.g. runs before #1 completes). Or that #1 has a fundamental flaw, like a transaction that hasn’t been committed.
Either the Category was created in a transaction that wasn’t committed, or the new Category ID you are using is somehow incorrect, or your foreign key setup is wrong, or as others have mentioned the Category creation is async but wasn’t awaited properly.
The easiest debugging step I can think of is, directly after creating the category, query that it exists with the returned new ID. If it doesn’t exist, then you know your problem is right there with the creation code.
Are you using EntityFramework? If so, what does the entry configuration look like? Are you calling SaveChanges()
(or SaveChangesAsync()
) after creating the category? Are you seeing the foreign key directly on the Post entity, or setting the object?
Need a bit more information to give you an answer.
Entity Framework. Save Changes Async. Using foreign key.
Are you attaching entities to context when you “use” them after creation?
If you’re loading existing entity to context then do you have a tracking context (it will keep track of loaded entities and you can use those entities as direct object references instead of raw id’s).
Show us how you “involve more than one entity” and mapping for the properties that are changed at that stage.
Check for race conditions. Are you calling each part asynchronously? If so it sounds like this should definitely be done synchronously
I am calling everything asynchronously. Is that wrong?