Reputation: 267
I am using Entity Framework Database First for working with db.
I have a base class EntityBase
public class EntityBase
{
public Int32 Id { get; set; }
}
I have some other classes that are generated by EnitytFramework and represents tables of my db, for example, "User" class:
public class User
{
public int32 Id{get;set;}
public class Name{get;set;}
public class Age{get;set;}
}
I want all EF model classes to be inherited from EntityBase:
public class User : EntityBase
I can do this manually, by deleting Id field from User class and defining inheritance, but after I am updating model from db, all manually-made changes dissapears. Is there any way to keep or automatically add inheritance to EF model classes?
Upvotes: 2
Views: 2360
Reputation:
EF database first work with t4 files if you change manually the class, save and compile, T4 will generate the classes again and you lose your changes. You must make this change in T4 template. Search the EntityClassOpening method and change the last line:
_typeMapper.GetTypeName(entity.BaseType)
with this code:
_typeMapper.GetTypeName(entity.BaseType) ?? "EntityBaseClass"
Upvotes: 4