Edmx dependency for Entity Framework v4/5

Posts   
 
    
basik
User
Posts: 123
Joined: 20-Aug-2012
# Posted on: 06-Dec-2012 13:16:52   

Using the Entity Framework v4 template in LLBLGenPro 3.5. We are trying to evaluate the advantages of using LLBLGenPro instead of the VS designer. The VS designer produces an edmx file with a designer.cs which contains context and the entities.

LLBLGenPro produces the edmx(no designer.cs as this would duplicate entities) and separates the entities(these seem to be POCO classes) into a model project and the dbcontext into a persistence project. I would like to know why the edmx is needed at all.

Where does the persistence layer(DbContext) check that the length of a field in the database is not exceeded? The Edmx contains the database metadata, but I can't see how the Edmx is used.

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 06-Dec-2012 17:30:55   

The VS designer combines the designer specific info (where entity objects are placed in the diagram) with the actual model information inside an EDMX file.

We only have the model information in the EDMX file. The DbContext class loads the EDMX file at startup and builds with it the internal model for building queries etc.

So you can see the EDMX file (it's xml, you should take a look at it simple_smile ) as the mapping file for Entity framework. This is also the reason why it's in the persistence project, as it's used by the context.

In 'code first' (which we don't support, as it's not something one would need a designer for wink ), you have code and a dbcontext, and no edmx file. Instead, the dbcontext builds the edmx internally from the code and the conventions you might have defined.

So in all cases, the edmx file (or better: the EDM model inside it) is required for EF to function.

What we generate is code which is compatible with what the ef designer generates however we have split it into multiple projects (as you then have more flexibility as you can re-use the model project without dragging the context along). It simply utilizes the entity framework classes, e.g. dbcontext and the like.

Frans Bouma | Lead developer LLBLGen Pro
basik
User
Posts: 123
Joined: 20-Aug-2012
# Posted on: 04-Jan-2013 14:55:44   

My client has a requirement to use Entity Framework, but they want to use Code First - no EDMX - to ostensibly avoid merging issues with the xml. Yeah, I know there are probably endless debates to be had here. But that is how it is.

Microsoft provided an EF Reverse Engineer tool to allow a starting point project without an EDMX, by pointing at a database(so really pseudo 'code first' using database first!): http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/

There are other Visual Studio T4 templates which help achieve the same thing.

I'm now in the unfortunate position where LLBLGenPRO is the better tool, but doesn't support the 'code first' pattern out the box. To use the entity modeller would be too tedious for even a modest number of tables.

So my questions are: Is there any intention to support 'code first' in this way? Would it be feasible to create our own templates to produce the 'code first' pattern? It's really the mapping layer which is missing.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 05-Jan-2013 06:01:31   

basik wrote:

My client has a requirement to use Entity Framework, but they want to use Code First - no EDMX - to ostensibly avoid merging issues with the xml. Yeah, I know there are probably endless debates to be had here. But that is how it is.

Microsoft provided an EF Reverse Engineer tool to allow a starting point project without an EDMX, by pointing at a database(so really pseudo 'code first' using database first!): http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/

Just curious: What is the benefit on that? You generate Code-First code from a DB, then that generated code is used to generate the DB. disappointed ¿?

basik wrote:

Is there any intention to support 'code first' in this way?

AFAIK, it's planned but not decided in which version it will be released.

basik wrote:

Would it be feasible to create our own templates to produce the 'code first' pattern? It's really the mapping layer which is missing

You can write your own templates. You start either with model-first or db-first, if you start with the model, you can create the mappings for a specific DB. Then generate the code based on your code-first templates.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 05-Jan-2013 10:59:26   

We have plans to support code first for EF, but when / if hasn't been decided, so it won't be anytime soon.

The dbcontext preset generates a dbcontext class + poco entities and an EDMX. You can re-use those. What you need additionally are templates which generate the mapping code, which can be tedious as every difference between an entity class and the target table/view has to be covered by a mapping class. this is similar to what we ship for nhibernate, namely the fluentnhibernate templates, but fluentnhibernate is less cumbersome than EF's code first mapping code.

Frans Bouma | Lead developer LLBLGen Pro
basik
User
Posts: 123
Joined: 20-Aug-2012
# Posted on: 07-Jan-2013 11:32:00   

Thank you for the quick responses. I understand that it seems like the wrong approach to want a code first code pattern using the database first, but isn't that how it goes. Just when you think no one would do that, someone does!

I've had a look at the mapping code produced by the EF Reverse Engineer helper on their sample and a typical mapping class looks like so.


    public class CourseMap : EntityTypeConfiguration<Course>
    {
        public CourseMap()
        {
            // Primary Key
            this.HasKey(t => t.CourseID);

            // Properties
            this.Property(t => t.CourseID)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

            this.Property(t => t.Title)
                .IsRequired()
                .HasMaxLength(100);

            // Table & Column Mappings
            this.ToTable("Course");
            this.Property(t => t.CourseID).HasColumnName("CourseID");
            this.Property(t => t.Title).HasColumnName("Title");
            this.Property(t => t.Credits).HasColumnName("Credits");
            this.Property(t => t.DepartmentID).HasColumnName("DepartmentID");

            // Relationships
            this.HasMany(t => t.People)
                .WithMany(t => t.Courses)
                .Map(m =>
                    {
                        m.ToTable("CourseInstructor");
                        m.MapLeftKey("CourseID");
                        m.MapRightKey("PersonID");
                    });

            this.HasRequired(t => t.Department)
                .WithMany(t => t.Courses)
                .HasForeignKey(d => d.DepartmentID);

        }
    }

Helpfully enough, the EF Reverse Engineer Code First helper provides the T4 templates used to produce the mapping classes. I'll report back on how I get on with creating a custom template for this.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 07-Jan-2013 21:16:06   

Good luck,

I'll have to close the thread for now, and it will be opened again once you post a reply with your findings.