Backing fields that participate in seeding not work properly, when we use them for transforming (change/reformat) the data that coming from or going to the database #25459
Labels
area-migrations-seeding
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-bug
Milestone
Hello,
I have a simple sample code that shows a problem with backing fields when we use them in seeding, while we use them also to transform (change/reformat) data coming from or going to the database.
This is our
AppDbContext
class.And this is my
Entity
class.We have the
Gender
enum
type that transforms our entityName
base on the gender of the person (as you can see in theget
accessor of theName
property in the above code).As you can see in our
AppDbContext
we seed our database with an instance of ourEntity
like this:After this, we add our initial migration (
dotnet ef migrations add Initial
). And then update our database (dotnet ef database update
)What we expected is that we have 1 row of data like this in our database(
People
table):But real data in our database is this row:
•••••••
According to this code in our entity class:
EF core didn't see
_name
backing field and get back his data fromget
accessor ofName
property (addMr.
to the name of the person based on the gender)!Where is the backing field in this scenario?
This is while when we add a new person to our database in our
Program.Main
method we don't have this problem (EF core see and use our_name
backing field, instead of theName
property)This is our
Program
class code:When we run our code output is:
The text was updated successfully, but these errors were encountered: