I have amended the MyContext.tt
T4 template so that all generated POCOs implement IDataEntity
:
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
<#=codeStringGenerator.EntityClassOpening(entity)#> : IDataEntity
{ ... }
Is it possible to generate POCOs so that they implement a generic IDataEntity<T>
where T
is the type of the primary key column for the given table?
For example, if the primary key of the Customer
table is a Guid
, then the generated class would be:
public class Customer : IDataEntity<Guid>
{ ... }
I am using Entity Framework 6.
In the MyContext.tt
, I made the following changes:
Added a new method to the CodeStringGenerator
class:
public string PropertyTypeName(EdmProperty edmProperty)
{
return _typeMapper.GetTypeName(edmProperty.TypeUsage);
}
Added the following declaration:
<#
// Determine the type if the "Id" column for the DomainEntity<T> base type
var idProperty = typeMapper.GetSimpleProperties(entity).SingleOrDefault(p => p.Name.Equals("Id"));
#>
And in the class declaration:
<#=codeStringGenerator.EntityClassOpening(entity)#> : IDataEntity<<#=codeStringGenerator.PropertyTypeName(idProperty)#>>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments