0

As you know, when we generate a model using the entity framework those files are automatically generated and manual changes will be overwritten if we change the model.

//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

My question is, how can we keep those manual changes in place?

[DataType(DataType.Date)]
[Required(ErrorMessage = "Please enter a date")]
public Nullable<System.DateTime> date { get; set; }

It's pretty hard to maintain any manual changes, like the one above, made to those classes.

What kind of strategy would you use to solve this kind of problem?

cap7
  • 482
  • 5
  • 24

1 Answers1

4

Create a new file and add a partial class with same name as your generated entity.

You can see an example here. More about partial classes you can read on MSDN

Let's say EF generated next class

//------------------------------------------------------------------------------
// <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>
//------------------------------------------------------------------------------

MetadataType(typeof(EmployeeMeta))]
public partial class Employee
{ 

}

than in your project create a new file with same signature and same namespace

public partial class Employee
{
     //custom logic here
}
Artiom
  • 7,694
  • 3
  • 38
  • 45