Question

I have added a custom property to the Properties dialog of the Entity Framework 5 designer through

http://msdn.microsoft.com/en-us/library/microsoft.data.entity.design.extensibility.ientitydesignerextendedproperty(v=vs.103).aspx

This works well, the property appears in the Properties dialog and it is saved in the EDMX file.

Now I'd like to use that property in the DDL generation process. I have edited the T4 template file SSDLToSQL10.tt (found at C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen).

However, the custom property doesn't seem to appear anywhere in the metadata tree. The website (in German)

http://www.databinding.net/en/blog/post/2010/12/13/entity-framework-4-erweiterte-eigenschaften-in-einer-t4-vorlage-verwenden.html

tells me that the extended property should appear in the EntityType.MetadataProperties collection, but this collection contains only the following members:

KeyMembers Members Name NamespaceName Abstract BaseType DataSpace MetadataProperties

None of those is my custom property.

Am I missing something? How can I access the IEntityDesignerExtendedProperty's value in the T4 code generation template?

EDIT: Here is the EDMX part with the custom property:

<edmx:ConceptualModels>
  <Schema ...>
    ....
    <EntityType Name="Entity1">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Type="Guid" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="None" />
      <Property Type="String" Name="Name" Nullable="false" />
      <a:MyNewProperty xmlns:a="http://schemas.tempuri.com/MyNewProperty">True</a:MyNewProperty>
    </EntityType>

I guess I have to map that custom property from CSDL to SSDL somehow.

Was it helpful?

Solution 2

I was able to achieve what I want using the edmx:CopyToSSDL=true attribute:

<edmx:ConceptualModels>
  <Schema ...>
    ....
    <EntityType Name="Entity1">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Type="Guid" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="None" />
      <Property Type="String" Name="Name" Nullable="false" />
      <a:MyNewProperty edmx:CopyToSSDL="true"
                       xmlns:a="http://schemas.tempuri.com/MyNewProperty">
          True
      </a:MyNewProperty>
    </EntityType>

This way, the transformator that generates the SSDL from the CSDL copies the annotation over to the SSDL, so I'm able to access it in the T4 template that generates the DDL SQL file.

If someone is going to use this in Entity Framework 5, please not that there is a bug (http://entityframework.codeplex.com/workitem/702), and you can workaround by using the old EDMX XML namespace:

<a:MyNewProperty edmxv2:CopyToSSDL="true"
                 xmlns:a="http://schemas.tempuri.com/MyNewProperty"
                 xmlns:edmxv2="http://schemas.microsoft.com/ado/2008/10/edmx">
    True
</a:MyNewProperty>

OTHER TIPS

You added the property to the CSDL (conceptual layer) while the DDL is created using the SSDL (store layer). You should be able to access the conceptual model in the SSDLToSQL10.tt but I don't think it is really what you are after. In general your property is not something the EF runtime can really use - I believe it will be just treated as an extension and ignored. If you want to add a property that is supposed to be used by the EF runtime the property must be declared in the CSDL (conceptual layer) and SSDL (store layer) and mapped correctly in the MSL (mapping layer) - with the latter being probably the most difficult. Unless I am missing what you are trying to achieve you are probably using a wrong extension point. The IEntityDesignerExtendedProperty allows defining custom properties that shows in the property and the model browser windows in the designer but are ignored at runtime. For me it looks like you would like to add a property automatically to your model. For that I would try using the IModelTransformationExtension where you should be given the entire edmx which you will be able to modify at will (i.e. CSDL, SSDL, MSL and add elements (properties) in correct EF xml namespaces). I would try using OnBeforeModelSaved since I believe the model will be saved automatically before trying to generate the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top