WJK
WJK

Reputation: 683

Adding the SQL Designer Table and Column Description as the class and property descriptions in Entity Framework 5

I would like to do the same as described in this post: Entity Framework 4.1 dynamically retrieve summary for scalar properties from database table column description

I noticed that the user stated that he is using Entity Framework 4.1. Can anyone confirm whether this feature is part of Entity Framework 5 or 6? If not, do anybody know of someone that might have added this feature in EF 6, since it is open source now?

Summary

I have added descriptions to the columns and tables in MS SQL Designer. Instead of having to retype these as xml comments for my classes and properties in the generated Entity Framework classes, I would like to have these xml comments generated from the SQL descriptions. Would something like this not be possible with a T4 template? My Entity Framework knowledge is very lacking, so if someone can even just point me in the right direction, I am willing to figure this out on my own.

Any advice would be greatly appreciated!

Upvotes: 0

Views: 706

Answers (1)

WJK
WJK

Reputation: 683

Okay, I have figured out an alternative way. It's not the answer to the original question, but it will work just as well. What I have done was entered all the Descriptions from my MS SQL database into the Entity Models Documentation.Summary and Documentation.Long Description.

Then I looked at some code from these two posts:

Example1
Example2

and altered it slightly to work like this:

In the "YourEFModelName".tt file (NOT "YourEFModelName".Context.tt), I looked for this line:

<#=codeStringGenerator.EntityClassOpening(entity)#>

to find the class header. Then I added this right in front of it:

    <#if (!ReferenceEquals(entity.Documentation, null))
{
#>
/// <summary>
/// <#=entity.Documentation.Summary#>
    <#if (!ReferenceEquals(entity.Documentation.LongDescription, null) && !ReferenceEquals(entity.Documentation.LongDescription, ""))
    {
    #>
/// <#=entity.Documentation.LongDescription#>
    <#}#>
/// </summary>
<#}#>

Note that the argument to the "EntityClassOpening" method is "entity", as used in the code you insert into the tt file.

You can use this code for the properties also. To find the properties, just look a bit lower down in the code for this line:

<#=codeStringGenerator.Property(edmProperty)#>

It will generate all your class properties. You can alter the code we used for the class comments to have "edmProperty" (the parameter to the "Property" method in the previous line) instead of "entity".

Then do the same for the Complex Properties and Navigation Properties found just below the edmProperties.

Once I added all my comments into the Entity Framework model and made these changes to the tt file, I right clicked the tt file and chose "run custom tool".

All my generated classes had xml comments with the descriptions I entered in the documentation property of all the classes and properties in my model.

My thanx goes to the people that contributed to the two example threads!

Upvotes: 1

Related Questions