Reputation: 1509
Can i change entity framework database first auto generated classes (under .tt) to derive from a base class (BaseEntity)?
Some of my domain classes has two property (CreateDateTime & CreateUserId) and i want to set this properties automatically before SaveChanges() with the following code:
private void AuditFields()
{
foreach (var entry in this.ChangeTracker.Entries<BaseEntity>().Where(x => x.State == System.Data.EntityState.Added))
{
entry.CreateDateTime = DateTime.Now;
entry.CreateUserId = CurrentUser.Id;
}
}
in top of those partial classes i saw the following warning:
Manual changes to this file may cause unexpected behavior in your application.
Upvotes: 1
Views: 1519
Reputation: 11338
are you planning then to go code first? Will you need to regenerate ever again ?
If you know the answer to those questions. Or have at least considered the impact.
See Reverse engineer code first to existing DB
This gets you a model you can start with
OtherWise: The partial class approach may help. Last time i tried (EF4) the generated code was partial. You leave the generated class alone and add a partial section
public partial class MyGeneratedClass : SexyBaseObject
{
//...
}
Upvotes: 1