Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the Entity data model designer use my database column descriptions?

I am using EF4 with Visual Studio 2010. I have a SQL database already created and all my columns are documented with the Description property in SQL management studio. When I create the edmx in Visual Studio, how can I make it read those descriptions and use them to add comments to my generated classes? I know I can use the Summary and LongDescription in the edmx properties but I'm looking for a way to do this without having to copy and paste every description.

Thanks!

like image 891
Jack Avatar asked Apr 30 '10 21:04

Jack


People also ask

Which option should be chosen to create an entity model?

Entity Data Model Wizard in Visual Studio (2012/2015/2017) opens with four options to select from: EF Designer from database for the database-first approach, Empty EF Designer model for the model-first approach, Empty Code First model and Code First from database for Code-First approach.

What are the parts of the entity data model in Entity Framework?

EDM (Entity Data Model): EDM consists of three main parts - Conceptual model, Mapping and Storage model. Conceptual Model: The conceptual model contains the model classes and their relationships. This will be independent from your database table design.


3 Answers

There is a feature request for this. Feel free to add your votes to help make this available in the future: Retrieve the SQL Descriptions in Entity-Framework feature request

Huagati has some great tools for working with EF and L2S. One of the features is updating the EF documentation based on the SQL database: Huagati website

From the website: Update ADO.NET Entity Data Model documentation from database retrieves free-text table and column descriptions, and index definitions from the database and updates the xml documentation fields in the EDMX designer with the descriptions.

It seems they look for these fields in the database and then update the model XML directly. Probably someone could create a VS Add-In that would do the same without the price if this is the only feature you wanted. I'll add this to my list of "future" projects (though I never seem to find time for these!).

Hope that helps!

like image 163
Matt Penner Avatar answered Nov 16 '22 14:11

Matt Penner


I've been looking at ways of hacking something together to populate the edmx with the meta data from the database.

The summary and long description edmx properties are stored in elements under the EntityType element.

<EntityType Name="EntityName">
    <!-- Without this element classes are typically generated with 
         "No Metadata Documentation available." -->
    <Documentation>
            <Summary>Entity Summary</Summary>
            <LongDescription>Entity Long Description</LongDescription>
          </Documentation>
    <Key>
      <PropertyRef Name="Id" />
    </Key>
    <Property Name="Id" Type="String" Nullable="false" MaxLength="25" Unicode="false" FixedLength="true" />
    <!-- Lots of other properties -->
  </EntityType>

The relevant section of the edmx file, the Store Schema Definition Language (SSDL), is created by System.Data.Entity.Design.EntityStoreSchemaGenerator.GenerateStoreMetadata(). Or at least this is the case with EdmGen.

I'm going to see if the EntityStoreSchemaGenerator.StoreItemCollection can be modified before being used by EntityStoreSchemaGenerator.WriteStoreSchema(...) to output the XML.

Update

Well that was annoying. System.Data.Metadata.Edm.Documentation is sealed and only has an internal constructor. Both properties of interest can only be set internally as well. So it seems like a dead end approach.

like image 38
Daniel Ballinger Avatar answered Nov 16 '22 16:11

Daniel Ballinger


I don't know that the designer itself has any extensibility points. However, once the Summary and LongDescription fields are populated in your edmx file, those value will remain there, even if you make other changes or re-updated your model from the database. Unless you delete a table and re-add it, those values will remain populated.

So you could either just copy and paste them all in one at a time (how many tables are in your model? This goes quicker than you think), or write a program to extract the info from your database (using SQL SMO or something), and have that program edit your edmx file, populating the Summary and LongDescription fields (make a backup of your edmx each time you try your program -- you don't want to botch your edmx file and have to start over).

If you have large models, and you're making lots of them, writing a program to do it automatically is worth your time. If you've only got a few models, with not too many tables, copy paste it is.

You might want to think about submitting feedback to the Entity Framework team here. Seems like the designer should automatically pick up on the description field from SQL Server. Would make a good feature request.

like image 45
Samuel Meacham Avatar answered Nov 16 '22 16:11

Samuel Meacham