Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Add the ObjectSet for each table to the ObjectContext, each with it's field, property getter and creation.
  • 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.

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?

like image 231
bevacqua Avatar asked Sep 01 '26 15:09

bevacqua


1 Answers

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

like image 162
Justin Pihony Avatar answered Sep 03 '26 05:09

Justin Pihony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!