Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 26-Dec-17 4:17am

1 solution

No, it's not possible. You're going to have to have a method do this work if you're going to use Database First because the regeneration will overwrite the entire file. It will NOT touch your second file with the AgeOnDate stuff in it. The problem there is you can't override the generated code in the first (generated) file.

Using Code First instead of Database First would have avoided all of this. There's no regeneration because you write ALL of the code for the classes and for the database generation on the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900