64

I have an existing database. I was hoping there was a way to generate class files from this database. However, I seem to see a lot of generating the database from the class files.

Is there a way to generate class files from an existing database using the Entity Framework? If so how? Can someone point me to a tutorial?

user3340627
  • 3,023
  • 6
  • 35
  • 80
JavaScript Developer
  • 3,968
  • 11
  • 41
  • 46
  • See This http://stackoverflow.com/questions/9876619/entity-framework-5-0-beta-will-a-dbcontext-code-generation-template-be-provide – AmirHossein Nov 03 '12 at 19:57

4 Answers4

123

1) First you need to generate EDMX model using your database. To do that you should add new item to your project:

  • Select ADO.NET Entity Data Model from the Templates list.
  • On the Choose Model Contents page, select the Generate from Database option and click Next.
  • Choose your database.
  • On the Choose Your Database Objects page, check the Tables. Choose Views or Stored Procedures if you need.

So now you have Model1.edmx file in your project.

2) To generate classes using your model:

  • Open your EDMX model designer.
  • On the design surface Right Click –> Add Code Generation Item…
  • Select Online templates.
  • Select EF 4.x DbContext Generator for C#.
  • Click ‘Add’.

Notice that two items are added to your project:

  • Model1.tt (This template generates very simple POCO classes for each entity in your model)
  • Model1.Context.tt (This template generates a derived DbContext to use for querying and persisting data)

3) Read/Write Data example:

 var dbContext = new YourModelClass(); //class derived from DbContext
 var contacts = from c in dbContext.Contacts select c; //read data
 contacts.FirstOrDefault().FirstName = "Alex"; //edit data
 dbContext.SaveChanges(); //save data to DB

Don't forget that you need 4.x version of EntityFramework. You can download EF 4.1 here: Entity Framework 4.1.

algreat
  • 8,592
  • 5
  • 41
  • 54
  • I follow your instructions, I created both, `Model1.tt` and `Model1.Context.tt` but, I couldn't understand how am I suppose to do the 3 step. Can you help me out? I compiled to see if I got any result but no luck =( – Michel Ayres Feb 18 '13 at 13:46
  • Have you checked if your POCO classes and Context had been generated? or you get some errors on step 2? – algreat Feb 18 '13 at 14:14
  • Both are ok, I created the reference to EF using Nuget. No errors, Build Succeeded. – Michel Ayres Feb 18 '13 at 14:28
  • 1
    And you can see your generated classes? Ok. Then you should create an instance of your model class. YourModelClass is a class derived from DbContext. Is this step ok? – algreat Feb 18 '13 at 14:33
  • 7
    AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH Found it!!! it was "inside" the Model.tt (I didn't open the small arrow before) * - * But now, the 3 step is to use the classes right? – Michel Ayres Feb 18 '13 at 14:37
  • Yes. Now all manipulations with your classes/DB you do using dbContext. Like add/edit/remove items and save changes. – algreat Feb 18 '13 at 14:54
  • This answer is out-dated. Entity Framework 7.0 does not support the creation of EDMX models. – Mick Dec 21 '15 at 03:51
  • This is a super answer – powernit Mar 02 '17 at 17:59
16

I found very nice solution. Microsoft released a beta version of Entity Framework Power Tools: Entity Framework Power Tools Beta 2

There you can generate POCO classes, derived DbContext and Code First mapping for an existing database in some clicks. It is very nice!

After installation some context menu options would be added to your Visual Studio.

Right-click on a C# project. Choose Entity Framework-> Reverse Engineer Code First (Generates POCO classes, derived DbContext and Code First mapping for an existing database):

Visual Studio Context Menu

Then choose your database and click OK. That's all! It is very easy.

algreat
  • 8,592
  • 5
  • 41
  • 54
1
  1. Open the EDMX model
  2. Right click -> Update Model from Browser -> Stored Procedure -> Select your stored procedure -> Finish
  3. See the Model Browser popping up next to Solution Explorer.
  4. Go to Function Imports -> Right click on your Stored Procedure -> Add Function Import
  5. Select the Entities under Return a Collection of -> Select your Entity name from the drop down
  6. Build your Solution.
Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Shubh
  • 11
  • 4
1

EDMX model won't work with EF7 but I've found a Community/Professional product which seems to be very powerfull : http://www.devart.com/entitydeveloper/editions.html