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.
Related
We have data that is fairly static, and we load that data into an XPage for attributes or label names or combo/radio/pull-down dataValues.
Using Resource Files in the Resources section is fast and simple EL syntax.
But how to dynamically update the data in the Resource File became the challenge. This is how we gotter' done:
First, accessing a Resource File containing manually pasted key=value,[n]; paired-data, just to prove we can; we did like this:
<xp:this.resources>
<xp:bundle src="/dojoPivotData.properties" var="dojoPivotProperties"></xp:bundle>
</xp:this.resources>
The data in the 'dojoPivotProperties.properties' file looks like this:
Passing the variable name var="dojoPivotProperites" into a Custom Property of a CustomControl is done by changing the Type: of the Property Definition of the property by Pasting java.util.PropertyResourceBundle into the Type: property, like so:
And passing in the Resource var variable name to the Custom Property where the Custom Control in embedded looks like this:
How I used values from the Custom Property, defined in the Custom Control:
How is used the .properties file to find keys to get their values to use in repeat controls by doing computations on the value of the passed in .property file in a DataContext:
To get the Finále where the data is driving the design elements that get generated in the UI:
**
Special Note
**
The voyages associated with each Destination cell above is generated at load by using the same java.util.ResourceBundle .property file to grab each Destination keyName from the .properties file. Each keyName in the .properties file has a list of voyages associated with that keyName like so:
And those voyages associated with Africa are generated into an attribute-list for "Africa" square like this:
To get this HTML result in the browser:
Question: How do we load the keyName=Value pairs into the Resource File on the Fly?
Ans: See* "Dynamic .property Resources File on the Fly"
I will show how I did it in a post by that title
I have a Project that use EF 5.0, is data base first. I would like to pregenerate the views, so I try to use this temaplate:
Template
However, how I have one library that has the edmx and the reposutory and I have another assembly with my POCO entities, so the library that implements my repository has a reference to the assembly that has the POCO entities. But this is a problema, and I read a solution by Scott Stafford:
solution
In this solution he says:
"To work around this, I made up a new entity and put it in the context's assembly, and listed it as the first DbSet. Now it picks it up, and works well (except that this is ridiculous)."
However, I don't know where to create a new entity. This new entity must by in my library the has the edmx or in the assembly that has the POCO entities? Is it not needed to modify the template .tt?
Thanks.
I have created a DataBase in SQL and created an EDMX in Visual Studio 2012. It automatically created POCO (TT) classes. Everything looks fine.
Now I change the column name of a table. I update the EDMX. Opening the EDMX in XML and everything looks fine.
Question 1
After I ran Custom tool in TT, I see that a new property got created additionally, e.g.:
SQL table name : Student
Column name : sName
In my POCO Class
public int sName{ get; set; }
got automatically created.
Now I change the column name in SQL to
Column name : studentName
My POCO class
public int sName{ get; set; }
public int studentName{ get; set; }
Is this a bug or do I need to do something to fix this?
What should I do to avoid this?
Question 2
Also, if I change the data type of any SQL column and update the model from the DB in my EDMX designer, the Conceptual Model is not updated. How do I go about this?
First, to understand your problem, what you need to know is that the EDMX file is just an XML file that contains 3 different sections:
CSDL: Conceptual schema definition language
SSDL: Store schema definition language
MSL: Mapping specification language
The CSDL contains the entities and relationships that make up your conceptual model. The SSDL describes your DB model and the MSL is the mapping between the 2.
The “Update Model From DB” process will update the SSDL (change everything that is inconsistent with the current DB schema), it will only modify the CSDL in case you’ve added new things to your DB schema.
This is quite a normal behavior since your Conceptual schema may/should differ from your DB schema (unless you want your Domain model to look exactly like a DB model which obviously do not sound as OOP/DDD best practices).
As for #Peru, the solution would be to delete the concerned entity (not the entire EDMX!) and then perform the “Update Model From DB” process.
Hope this helps!
Edit:
There's a tool, not for free, which is a Visual Studio plugin that allows you apply changes made into the DB, both on the CSDL and SSDL files: Huagati DBML/EDMX Tools.
The only "free" solution is deleting the entity (or the right field within this entity) that needs to be updated.
Remember that CSDL is supposed to be maintained by developers and must look like an Object Model rather than a DB Model. Imagine you have setup inheritance between your entities or that you have split 1 DB table to 2 EDMX entities, running "Update model from DB" should not overwrite everything!
Personally, I'd open the edmx file as XML and find the problem node and delete it. The file is pretty easy to understand - don't be scared to at least try it out.
I know I'm a bit late here but there is a relatively simple way to generate your POCOs/update your DbContext from the EDMX designer.
Go to your designer, right click on the empty area, click "Update Model from Database", add/update your tables. This will update your edmx XML. After you've verified that your table has been added to the CSDL, SSDL, and MSL sections, right click on your T4 template in the solution explorer (<Name>.tt, not <Name>.Context.tt) and click "Run Custom Tool" - this will generate POCOs for any updated objects. Note that if you are creating new entities, they will not be added to your DbContext class, you will still have to do this manually by adding the following line at the bottom of your class: public virtual DbSet<Entity_Name_Goes_Here> EntityNames { get; set; }
Only the manual edit worked for a View in my project. (going from smallint to decimal(18,2) )
Open the .EDMX file in a text editor, find appropite section, and manually change the Property Type="..." value.
Once you update the fields in Db, Find the respective model.cs file then delete those fields from the model. now Update the EDMX file (Update Model from Database). It worked for me.
Created a Model class with DataContract and DataMember Name for each property in the class. The XML, JSON, JSV contents comes out with the Name as specified in the DataContract attribute. But CSV is not, it display the actual name of the property in the class.
Example header in XML, JSON, JSV:
FileNumber, AccountYear, AgreementDate, CollectionDate
Example header in CSV:
FILE_NO,ACCOUNT_YR,AGREEMENT_DATE,COLLECTION_DATE
I do not have any custom formatters / handlers for CSV, its all what comes with ServiceStack.
Is this how it is OR is any settings is required?
Thanks
The CSV serializer currently doesn't look at the Name property of the [DataMember] attribute.
Although if you add an issue on the ServiceStack.Text project, we can look at adding support for this in future.
I have a Core Data model with a single transformable attribute. I also have this attribute use a custom NSValueTransformer, setup in the model properly.
When I use mogenerator to generate/update my machine and human files, the machine files for the entity containing this attribute always type the attribute to NSObject. In order for Core Data to use my custom value transformer, this type needs to be the type the transformer understands. Right now, I manually do this in the human file by redefining the property with the proper type. This does the job and gets the transformer working. However, I end up with several compile warnings regarding redefinition of the attribute.
One of the more recent releases of mogenerator specified in the release notes that transformable attributes are now supported. However, I haven't found any example syntax to enable this feature.
This should be better documented.
To set your generated attributes's type, select your desired attribute in the modeler and switch to the User Info tab. Then create a new element with a key of attributeValueClassName and a value of whatever you'd like.
Here's a screenshot: