I have an application which has a server side part and a client side part. The server side part is implemented with WebApi2 and EF6 and uses Json AND Protobuf as serialisers.That's why I use [DataMember] as attribute on the properties of my models. Here I need to put the order into the Attribute [DataMember(Order = number)] because of the Protobuf serialiser.
From my serverside models (codefirst) I generate an edmx file which holds the information of the models. On the clientside I generate these models with a t4 template which uses the edmx file. Before I used Protobuf I just put the DataMember attribute on top of every property in the t4 template which worked fine. But now I have to use the same Order as on the server so that it still works with Protobuf. This means that I have to read the DataMember Attribute in the edmx and find the Order value. But I just can't read the attribute.
I tried to read the
edmProperty.TypeUsage.Facets
and some other properties on this object. I also searched on google and had a look at some sampleprojects on t4 templates. But no luck.
So how can I read the order of the attribute?
MyServerModel
{
[DataMember(Order=1)] //this number of the order is what I'm looking for
MyProperty{get;set;}
}
Now use the magic of T4 and generate following with the help of the edmx file.
MyClientModel
{
[DataMember(Order=1)] //I want to generate this Attribute with the t4 template from
//the edmx file
MyProperty{get;set;}
}
The DataMember Attribute is not part of the edmx file because every member is a DataMember attribute. And as a result of this the order information is lost. We have solved this problem in the following way: First we generate the edmx file normaly. Afterwards we parse it manualy and look for the EntityType entries. Then we load the Type with reflection from the Assembly the code first models reside in. Parse the CustomAttributes to find the DateMemberAttribute where we can read the Order. After we have this information we can write this into the edmx file by either putting it in the documentation tag (not so elegant) or add it the Property/NavigationProperty tag as custom annotation.
After this our edmx Property tags look similar to this:
<Property Name="PropertyName" Type="Guid" xmlns:d2p8:DataMemberOrder="5" xmlns:d2p8="http://www.yourcompany.com/customAnnotation" />
In the t4 template script you have to parse edmx file again for this information and you can write the DataMember annotation with order.