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

Using backing fields

From the course: Using Entity Framework Core with Legacy Databases

Start my 1-month free trial

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…

Contents