From the course: Using Entity Framework Core with Legacy Databases
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Using backing fields
From the course: Using Entity Framework Core with Legacy Databases
Using backing fields
- [Instructor] Sometimes it makes sense to have a public property on the data model perform logic in the setter. Maybe you wanna use a specific value when the property is set to null, or want to do some validation based on the other properties in your model. It could even be that the property doesn't have a setter because the value is read only in your application. It's likely that you don't wanna run this logic when Entity Framework is populating a model from the database. We can have Entity Framework populate the backing fields directly rather than using the public properties. Let's add some logic to the email setter of customer and see this in action. First we'll add a backing field for our public property. Now we'll change the getter to return that backing field and we'll change the setter to do our logic. In this case, we'd like to make sure new emails entered are in all lowercase. Now, if Entity Framework doesn't know about this backing field, the logic in the setter will be run…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
Why change the generated model2m 8s
-
(Locked)
Changing names1m 49s
-
(Locked)
Using shadow properties2m 23s
-
(Locked)
Generated properties and default values4m 34s
-
(Locked)
Using backing fields3m 35s
-
(Locked)
Adding and altering indexes3m 38s
-
(Locked)
Adding concurrency tokens and timestamps4m 8s
-
(Locked)
-
-
-