I’m having issues with a database uploadscript. It basically just adds all entities from a backup source to the database.
However, as some fields of the new database are non-existent in the old data source, I assign them default values. See screenshot below for examples, the string property CCP1_description (and the similarly named descriptions below that) are being set to "Enter description here."
However, when I save the entity, I get an Exception saying that the description field is not allowed to be NULL. Which it clearly isn’t.
See screenshot for code + Exception

Here’s a snippet from my database creation script, it shows how the columns have been set up:
...
[CCP1_description] nvarchar(max) NOT NULL,
[CCP2_description] nvarchar(max) NOT NULL,
[EP1_description] nvarchar(max) NOT NULL,
[EP2_description] nvarchar(max) NOT NULL,
[EP3_description] nvarchar(max) NOT NULL,
[EP4_description] nvarchar(max) NOT NULL,
[EP5_description] nvarchar(max) NOT NULL,
...
If I check my temp variable when debugging, I can clearly see the content of the description fields (all are ""Enter description here. like they should be.

I know that a lot of things can cause this exception; but I don’t see the root cause of this issue. Can someone shed some light on this? I have no idea why this exception is thrown…
I found many open questions with the same error, but I think this is a different situation because my fields are not databasegenerated, nor are they PK or IDENTITY fields. They are not empty when the entity is being constructed, and the object that is being added actually has the correct values.
UPDATE
I just noticed that the first 83 entities are added correctly. The exception is thrown on the 84th time the code is executed (and a lot of times after that.
Which means the issue is probably within another property field.
I checked all the fields, the only null values I encounter are added in fields that have been set nullable in the database and entity framework.
And now I really don’t get why an exception is thrown on the description fields.
UPDATE 2
It seems this application was coded by Salvador Dali.
I reworked the code, but this time, I commented out the .AddObject(temp); line. Which means the entities are created, but never added to the db context. The db.SaveChanges(); call should then actually be redundant and do nothing at all.
Or so I though. Turns out, my entities are still being added. I stepped through the entire code execution, it never uses the db context in any way except for the db.SaveChanges();, yet it still all gets added.
Conversely, if I only start adding the entities after a counter has reached 100 (which means only item 100, 101, 102, … will be saved, all the rest will not be added), it stil keeps adding the other entities as well.
I have no clue at all. Literally none. I’ll update the post once I know more.
FINAL UPDATE
I managed to find a workaround (see my answer below). However, I’m still looking for an explanation why this is happening. If anyone can explain this to me, I’ll award them the answer to this question.
I’ve managed to get a bit further in the code. I’ve changed the order of operations. It used to be:
Somehow, And I cannot explain this, the entitiy gets saved automatically. If I run the exact same code as above but without the
.AddObject()function, it still gets added.So I changed my code to:
This doesn’t really solve the original problem, it’s just a workaround. If anyone can provide me with an explanation why these entities are being added, I will accvept that as the solution to my problem.