Why is EF Core adding 1 to my column names?11 Jun 2018
Today I came across a really annoying issue which practically wiped out my whole day! >.<
I was undertaking an ambitious project whereby I’d map some pre-existing SQL tables to a new set of domain classes when I came across the following error…
at System.Data.SqlClient.SqlCommand.<>c.<ExecuteDbDataReaderAsync>b__180_0(Task`1 result)
For some context the domain classes I wrote were abstract with navigation properties on both the abstract classes, as well as the derrived classes.
To save your sanity I have recreated a very basic similar set of classes for your eye balls.
And the following is how the above classes were incorrectly configured.
So just to be clear this is what the SQL table would look if you were to allow Entity Framework to deploy the above domain model.
I never created a property called
I tried all the seemingly obvious things.
I added explicit column name fluent mappings. Nothing.
I added a reverse navigation property. Nothing.
So how do we fix this happy little miracle?
Well it turned out to be such a simple fix. The problem was with how my
DbContext had been configured.
In fact digging into the
EntityTypeBuilder source code explicity outlines this case.
Configures a relationship where this entity type has a reference that points to a single instance of the other type in the relationship.
Note that calling this method with no parameters will explicitly configure this side of the relationship to use no navigation property, even if such a property exists on the entity type. If the navigation property is to be used, then it must be specified.
I’m not 100% sure why the navigation expression defaults to null but hey ho.