So, you started a project with several Entity Framwork 4+ code first models. And you use data annotation attributes in combination with resource files to set your displaynames and error messages. Like so:

post1

Now, if you want to fill the resource file initially, it would be nice to copy paste something into it.. but this does not work. And I have 20+ models each with 20+ properties.. errrhh.

Solution is this.. run this SQL script on the database that is generated by EF:

DECLARE @name nvarchar(128)
DECLARE @col nvarchar(128)

DECLARE cur CURSOR FOR
SELECT TABLE_NAME + '_' + COLUMN_NAME as name, COLUMN_NAME as col
FROM INFORMATION_SCHEMA.COLUMNS
OPEN cur

FETCH NEXT FROM cur INTO @name, @col;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT '<data name="'+ @name +'" xml:space="preserve">'
PRINT ' <value>' + @col + '</value>'
PRINT '</data>'
FETCH NEXT FROM cur INTO @name, @col;
END

CLOSE cur;
DEALLOCATE cur;

This will create an output like this:

<data name="Contacts_Country" xml:space="preserve">
<value>Country</value>
</data>
<data name="Contacts_PersonalInterests" xml:space="preserve">
<value>PersonalInterests</value>
</data>
<data name="Contacts_Memo" xml:space="preserve">
<value>Memo</value>
</data>
<data name="Contacts_ImportantNotes" xml:space="preserve">
<value>ImportantNotes</value>
</data>
<data name="Contacts_Extra" xml:space="preserve">
<value>Extra</value>
</data>

And you can copy paste this in the xml from the resource file, open the .resx file with an xml editor and copy paste.
And your done in 3 minutes.

Hope this helps

Flores Eken

Author Flores Eken

I’m passionate about technology in general and particularly about software development and automation. Specializing in Infrastructure automation and Microsoft .NET, I can handle the entire process, from installing a Team Foundation Server to the smallest programming detail. Always innovating and on the cutting edge.

More posts by Flores Eken
24 September 2013

Leave a Reply