Entity Framework T4: POCOs implement generic interface?

Dave New

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.

Dave New

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Entity Framework T4: POCOs implement generic interface?

From Dev

Entity Framework 6.0 Decoupling Implementing From POCOs

From Dev

Implement Generic Abstract Entity class with dao interface and implemantation

From Dev

Why can't a Java Generic implement an Interface?

From Dev

Typescript Can't implement generic function interface

From Dev

Modifying Entity Framework Types T4 Template

From Dev

Modifying Entity Framework Types T4 Template

From Dev

Where are types used in Entity Framework T4 templates defined?

From Dev

implement generic generic interface in scala

From Dev

Implement Interface Generic Function

From Dev

Implement a generic interface

From Dev

Implement Interface Generic Function

From Dev

Entity Framework and adding POCOs without adding child objects

From Dev

Programmatically obtain Foreign keys between POCOs in Entity Framework 6

From Dev

Separating entity framework into a separate project and using POCOs in presentation layer

From Dev

How to implement a generic interface with a child generic interface

From Dev

How to implement a generic interface with a child generic interface

From Dev

Generic methods and Entity Framework

From Dev

Entity Framework 6 T4 Template change file name of entity

From Dev

T4 - Entity Framework Error: Method not found: 'System.Data.Entity.DbSet`1

From Dev

Modify T4 template from an ADO.NET Entity Data Model (Entity Framework)

From Dev

Dictionary of classes that implement a generic interface

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

Dictionary of classes that implement a generic interface

From Dev

How to implement an interface with generic types?

From Dev

How implement an interface Generic Method

From Dev

limit T in a generic class to types that implement some interface

From Dev

Create Entity Framework Without t4 code generator (like vs 2010)

Related Related

  1. 1

    Entity Framework T4: POCOs implement generic interface?

  2. 2

    Entity Framework 6.0 Decoupling Implementing From POCOs

  3. 3

    Implement Generic Abstract Entity class with dao interface and implemantation

  4. 4

    Why can't a Java Generic implement an Interface?

  5. 5

    Typescript Can't implement generic function interface

  6. 6

    Modifying Entity Framework Types T4 Template

  7. 7

    Modifying Entity Framework Types T4 Template

  8. 8

    Where are types used in Entity Framework T4 templates defined?

  9. 9

    implement generic generic interface in scala

  10. 10

    Implement Interface Generic Function

  11. 11

    Implement a generic interface

  12. 12

    Implement Interface Generic Function

  13. 13

    Entity Framework and adding POCOs without adding child objects

  14. 14

    Programmatically obtain Foreign keys between POCOs in Entity Framework 6

  15. 15

    Separating entity framework into a separate project and using POCOs in presentation layer

  16. 16

    How to implement a generic interface with a child generic interface

  17. 17

    How to implement a generic interface with a child generic interface

  18. 18

    Generic methods and Entity Framework

  19. 19

    Entity Framework 6 T4 Template change file name of entity

  20. 20

    T4 - Entity Framework Error: Method not found: 'System.Data.Entity.DbSet`1

  21. 21

    Modify T4 template from an ADO.NET Entity Data Model (Entity Framework)

  22. 22

    Dictionary of classes that implement a generic interface

  23. 23

    How to implement an interface with generic types?

  24. 24

    How implement an interface Generic Method

  25. 25

    Dictionary of classes that implement a generic interface

  26. 26

    How to implement an interface with generic types?

  27. 27

    How implement an interface Generic Method

  28. 28

    limit T in a generic class to types that implement some interface

  29. 29

    Create Entity Framework Without t4 code generator (like vs 2010)

HotTag

Archive