Pergunta

I have a custom interface IEntity. I want my Entity Framework generated POCO entities to derive from IEntity. For now; i am adding one partial class for each Entity Framework entity and define;

public partial class Person : IEntity
{
}

public partial class City : IEntity
{
}

Is there any more elegant way of doing this by reflection or IoC containers?

Note: I want to have the possibility of not implementing IEntity for some entities in some cases.

Note2: I dont want to edit any tt or edmx file.

Foi útil?

Solução

Go to T4 template which generates entities (Something.tt), find line

<#=codeStringGenerator.EntityClassOpening(entity)#>

This line generates class opening string for each entity, e.g.

public class Foo

And replace it with

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

Now generate entity definitions will look like

public class Foo : IEntity

You will also probably need to add namespace using, if IEntity defined in different namespace (a little weird case I think).

Outras dicas

You can always make an Item Template. So when you do Add New => Entity Type instead of say the normal class type. So if you create an "Entity Type" it comes with IEntity already implemented/inherited whatever you set up.

http://msdn.microsoft.com/en-us/library/tsyyf0yh.aspx http://visualstudiomagazine.com/articles/2012/08/20/visual-studio-custom-templates.aspx

Then its not "long term" generation, it will just generate it when you create the file but yet it will automatically be there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top