6

I was just reading Asp.net MVC3 tutorials (Models (Data))

On this page tutorial 4 of 10 on the ASP.NET website, it is shown that an entity diagram is created from code first classes. How to generate them?

jao
  • 18,273
  • 15
  • 63
  • 96
Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
  • possible duplicate of [Entity Framework Code First - Is there a way to create a model diagram from POCO classes.](http://stackoverflow.com/questions/4925684/entity-framework-code-first-is-there-a-way-to-create-a-model-diagram-from-poco) – Ladislav Mrnka Apr 25 '11 at 20:09
  • 1
    @ladislav: yes , but that answer didnt solved my problem, its just one line answer and iam unable to figure out, how to do that – Praveen Prasad Apr 25 '11 at 20:13
  • 3
    There IS a tool that creates a diagram from exactly the Entity Data Model generated by EF from your Code-First classes and relationships - see my answer: http://stackoverflow.com/a/12223054/11545. Reversing the database *might* work, but not necessarily with any database; plus, you'll get the database column names for properties, which aren't necessarily the same as your model's properties' names. – Cristian Diaconescu Sep 20 '12 at 18:43
  • Also possible duplicate if http://stackoverflow.com/questions/18658078/how-do-you-create-a-visual-model-of-entityframework-code-first. I really like Slauma's answer, very simple and effective. – Ehsan88 Aug 25 '15 at 11:59

4 Answers4

5

This can be done very easily by using the Class Diagram. Add New Item > Class Diagram. Then drag and drop your code first classes into the diagram from the solution explorer.

Brian
  • 91
  • 1
  • 5
4

A Class Diagram is OK, but it doesn't automatically show the relationships between classes. The slickest way that I have used is Entity Framework Power Tools. Their description of the tool:

When right-clicking on a C# project, the following context menu function is supported: 1) Reverse Engineer Code First - Generates POCO classes, derived DbContext and Code First mapping for an existing database.

Josh C
  • 7,461
  • 3
  • 24
  • 21
  • 3
    If you just want a simple, read-only view, right-click on your class inheriting from DbContext and select **Entity Framework » View Entity Date Model (Read-Only)** – Pakman Jul 08 '15 at 20:55
3

Assuming your schema has been created from the Code First classes you can reverse the db into a an edmx to visualise the Model. Any classes generated from this obviously won't be related to your Code First classes though.

Darren Lewis
  • 8,338
  • 3
  • 35
  • 55
  • 2
    yes i did that, but i dont think that's the actual way to do. – Praveen Prasad Apr 25 '11 at 20:14
  • @Praveen: There is no correct way to do that. EDMX cannot be created from code-first model (unless you write tool which will be able to do that). – Ladislav Mrnka Apr 25 '11 at 20:16
  • That is the way the diagram was created for the tutorial (using the Database First designer on the database created by Code First). There isn't yet a way to directly create a diagram from code written for Code First. – tdykstra Apr 25 '11 at 21:34
0

Create a copy of your project. Open the copy and add a new item/ADO.NET Entity Data Model. Edit the diagram for layout and print to a .pdf file. Delete the copy of the project.

Anytime you make a change you will have to re-create the diagram and edit the layout, but I can usually get through the whole process in about 15 minutes.

Joe
  • 4,143
  • 8
  • 37
  • 65