Change entity framework database first auto generated domain classes

c# ef-database-first entity-framework entity-framework-6

Question

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.

1
1
3/29/2014 10:41:08 AM

Accepted Answer

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 
{
//...
}
1
3/29/2014 12:19:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow