Click here to Skip to main content
13,347,671 members (52,796 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have added ADO .net Entity Framework to my project and so I see the EDMX file. When I added it, there was this pop-up (Entity Designer???) asking me to choose a datasource, and I had read that if pointed to an existing DB, the necessary Object classes/entities will be auto generated in my project. But, I closed the dialogue thinking that I will set the datasource later. So how do I set the datasource and get it to autogenerate the entities now, after closing the dialogue?

Is there a way to get ADO .net entity framework to autogenerate the entities using a pre/post build event, or maybe from code behind?
What if I embed a blank edmx into a class library and compile the library, then refer the library in my main (parent) project, can I get it to autogenerate the entities corrosponding to the parent project's database through code by writing something like:

somewhere in the parent project start/entry point, then run the parent project so that this gets executed and creates the necessary files, after which I comment out the above line so that successive builds and runs do not generate/modify the entities again?

OP's additional information moved from non-solution below
dear prataph,
Mine is a DB first approach.
Say, i have a DB with a table named person.
It has 2 columns:
VARCHAR FirstName & INT Age

Now, in our program, we will create an object class
Class Person
  string _firstName;
  int _age;

  public string FirstName
    get{return _firstName;}
    set{_firstName = value}

  public string Age
    get{return _age;}
    set{_age = value}

or something similar. When the DB is complex and has many tables and columns, it gets difficult to create these classes.

I read somewhere that ADO .Net Entity Framework can automatically generate these object/class .cs files for you. That kind of makes a developers work easier and his/her time more productive...

How can this 'autogenerate object class' functionality of ADO .net be called from code behind?
Or can it be called only from the visual studio UI by right-click or when adding a new .edmx file?
Posted 23-Nov-12 3:59am
Updated 23-Nov-12 10:21am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

If you want to use the existing database tables in your project then forget about AutoGenerating Entities without using the designer wizard which pops up when .EDMX file is added to the project.

So if you choose an empty .EDMX file, then right click on the designer and click on Update model from database so that you can choose the required tables and stored procedures when needed.

Below i had explained how to create entities and there is a site for beginners, if needed go through it[^]

You can have multiple modeling techniques using Entity Framework 4.1 like code first, model first or database first.

Code First ------------ Model First ------- Database first


In Code First approach, you avoid working with visual model designer (EDMX) completely. You write your POCO classes first and then create database from these POCO classes. Developers who follow the path of Domain-Driven Design (DDD) principles prefer to begin by coding their classes first and then generating the database required to persist their data.

One important thing to understand is that there are two new types introduced for Code First approach, DbContext and DbSet. DbContext is a simplified alternative to ObjectContext and is the primary object for interacting with a database using a specific model. DbSet(Of TEntity) is a simplified alternative to ObjectSet(Of TEntity) and is used to perform CRUD operations against a specific type from the model in Code First approach.


In Model First approach, you create Entities, relationships, and inheritance hierarchies directly on the design surface of EDMX. So in Model First approach, when you add ADO.NET Entity Data Model, you should select ‘Empty Model’ instead of ‘Generate from database’.

After creating required entities, associations and inheritance on design surface of the empty model, you can use designer’s context menu option ‘Generate database from model’. However don’t get confused with name. it will not generate new database from model. It will only give you DDL to execute in existing database.


We have seen this approach in our first EDM sample where we created model and classes from existing database. So when you generate EDMX from existing database then it is a Database First approach.
Nelek 23-Nov-12 15:21pm
OP wrote you in a non-solution. Content was moved to question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180111.1 | Last Updated 23 Nov 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100