Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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:
C#
myLibrary.myEntityModel.functionToAutoGenerateEntities();

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
Person.cs
C#
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
Updated 23-Nov-12 9:21am
v3

1 solution

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 http://www.entityframeworktutorial.net/[^]


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


CODE FIRST -http://blogs.msdn.com/b/adonet/archive/2009/06/22/feature-ctp-walkthrough-code-only-for-the-entity-framework.aspx
-------------------

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.

MODEL FIRST
----------------------

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.

DATABASE FIRST
----------------------------

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.
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900