bevacqua
bevacqua

Reputation: 48566

Database First + EF + POCO; T4 Code Generation?

I'm using a DB first approach, using Entity Framework and I've just read this article about POCO In EF. Now: How can I generate the POCO templates directly from the database?

namespace My.Domain
{
    public class Entity
    {
        public virtual long EntityId { get; set; }
        public virtual string Example { get; set; }
        public virtual long NextId { get; set; }
        public virtual bool Lame { get; set; }
    }
}

In fact I would like it very much to being able to generate the same stuff I generate manually, in just one step:

Is there a "simple" way to accomplish this using T4 templates, or is there some other tool available to do just this? Perhaps some kind of Custom Tool instead of the one that comes with EF, but that is POCO-oriented?

Upvotes: 3

Views: 3943

Answers (2)

Believe2014
Believe2014

Reputation: 3964

Create POCO entities, preferrably in another project, with all the property mappings including relationships, with properties marked as virtual to enable proxying, and obviously in a namespace of my choice.

Entity Framework default templates cannot be placed in a separate assembly because it will need configurations and the EDMX file.

Is there a "simple" way to accomplish this using T4 templates, or is there some other tool available to do just this?

Yes, the Entity Interface Generator

https://entityinterfacegenerator.codeplex.com

This project contains customized T4 templates which can generate interfaces and attributes for the DbContext class and entity classes.

Upvotes: 0

Justin Pihony
Justin Pihony

Reputation: 67135

As long as you are using EF 4.1, then getting POCO objects generated is "built-in". Please refer to this article:

http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-model-amp-database-first-walkthrough.aspx

which, was just a piggy back to the older, more long-winded way found in

http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx

The first method is more of the suggested method now, but I figured I would also give you the older one anyway :)

Hopefully, this fits your needs :). I am not sure about creating it in your specified namespace and project. That sounds more like Code First (which you MIGHT be able to do and wire perfectly to the database?), but you might be able to bend the generation item to your whim. This should be a push in the right direction, though

Upvotes: 2

Related Questions