문제

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.

도움이 되었습니까?

해결책

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 
{
//...
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top