Using EF6 and VS2015 with a Database First model
One of the tables is "Customer" with one of its fields being the DOB as Date.
The EF generates the model and creates the Customer.vb file with contents
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated from a template.
'
' Manual changes to this file may cause unexpected behavior in your application.
' Manual changes to this file will be overwritten if the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
Imports System
Imports System.Collections.Generic
Partial Public Class Customer
Public Property Id As Integer
Public Property LastName As String
Public Property FirstName As String
Public Property DOB As Date
Public Property Added As Date
Public Overridable Property Visits As ICollection(Of Visit) = New HashSet(Of Visit)
End Class
What I want to achieve, if possible, is to run the method
Public Function GetAgeFromDates(dob as Date, optional someDate as Date = Date.Now
and store the return value in the variable
Private _AgeOnDate As Integer
Public ReadOnly Property AgeOnDate As Integer
Get
Return _AgeOnDate
End Get
End Property
when the DOB value is changed.
What I have tried:
EntityFramework warning, at the beginning of the Customer.vb file says
' Manual changes to this file will be overwritten if the code is regenerated.
Wanting to extend the, EF-auto-generated, Customer class and protect these changes from any model regeneration, I have created a separate file named CustomerExtended.vb with contents like
Partial Public Class Patient
Private _AgeOnDate As Integer
Public ReadOnly Property AgeOnDate As Integer
Get
Return _AgeOnDate
End Get
End Property
End Sub
Now my question is:
Is it possible, somehow, to have, somewhere in CustomerExtended.vb, an extended version of Customer Setter like:
Public Property DOB As Date
Set(value As Date)
_DOB = value
_AgeOnDate = GetAgeFromDates(value, Date.Now)
End Set
End Property
To make it short:
Is it possible to over-write the Setter of an EF-auto-generated property AND protect this code-extend from losing it on each model regeneration?