Question

We have the following part in our T4

<#=codeStringGenerator.EntityClassOpening(entity)#> : IEntity

Now since not all of our entities use type int as there primary key. I would like to make this interface generic. So we can have int, guid, long, short as the type. (Changing the types is not possible, legacy database)

something like

public IEntity<TId>
{
     TId Id { get; }
}

How can I now in the (default) entity framework T4 what the type is of the primary key? Is there a way to access the primary key in the T4?

Or is the only option using partial classes ? (I don't want to use partials for this. Because every time a class is added to the model, developers have to know about the interface and implement it on a new partial class)

Was it helpful?

Solution

You can use the KeyMembers property

http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.entitytypebase.keymembers.aspx

Then providing you have only 1 key you can retrieve that and use DeclaringType property of the EdmMember Class.

http://msdn.microsoft.com/en-us/library/system.data.metadata.edm.edmmember.aspx

As a side note I would probably consider extending EntityClassOpening method to add your generic interface on to the opening declaration

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top