2

I am using the Entity Framework to generate data access code from an old database.

The database table names and properties are all named in capitals with _ to separate words ie. CLIENTCLIENT_NAMED_CLIENT_ID etc.

I have written a class to transform these into camel cased strings:

public static class Extensions{
    public static string FirstCharToUpper(this string input)
    {
        return input.First().ToString().ToUpper() + input.Substring(1).ToLower();
    }

public static string CamelCase(this string input)
    {
        return input.Split('_').Where(a=>!string.IsNullOrEmpty(a)).Select(a=>a.FirstCharToUpper()).Aggregate((a,b)=>a+b);
    }
}

I am invoking this from my tt files and I have got to the point where my data classes and DbContext naming is the way I want it.

However I now get an error when I try to create a controller: 'Unable to retrieve metadata for myNamespace.Client'. Could not find the CLR type for 'myModel.ENTITYNAME'. (in capital)

To fix this, I made my Data.tt decorate my data classes with [Table("ENTITYNAME")] and my properties with [Column("COLUMNNAME")] - however this did not make any difference.

What am I missing?

2
  • You can edit the edmx file with notepad and update the CSDL, SSDL and MSL if the entities and fields are less in count. Jun 21, 2018 at 11:16
  • That is not ideal... It is a large db. I want to change the tt templates... it should just work. i'm sure i'm missing something simple.
    – Leo
    Jun 25, 2018 at 10:16

1 Answer 1

0
+50

Are you updating it the same way in all three (or two corresponding) layers: CSDL/MSL/SSDL? I would suspect there is a mismatch between two of the layers.

Possible useful in this case - here is a library I wrote for creating/updating/manipulating EDMX files a bunch of years ago: https://github.com/KristoferA/HuagatiEDMXTools/

If you use an older version of Visual Studio (2013 or older) then I also have a free VS addon/plugin that adds renaming and db<->model sync etc. You can download it from here: https://huagati.com/edmxtools/

Update: based on the comments below, I think you are renaming classes and properties in the generated code without making the corresponding change in the CSDL.

Instead of changing the generated code / tt templates: change the names in the CSDL and the references to those CSDL objects in the MSL. Then the default templates will generate code with the class/property/etc names you want.

7
  • I have changed the *Data.tt file and the *.Context.tt file to use the CamelCase naming. I didn't change the EDMX file.
    – Leo
    Jun 25, 2018 at 11:22
  • I'm using VS2015
    – Leo
    Jun 25, 2018 at 11:23
  • Ok, so the names used in the generated code does not match the names in the CSDL? If so, that would be the problem...
    – KristoferA
    Jun 25, 2018 at 11:35
  • Instead of changing the generated code / tt templates: change the names in the CSDL and the references to those CSDL objects in the MSL. Then the default templates will generate code with the class/property/etc names you want.
    – KristoferA
    Jun 25, 2018 at 11:44
  • That is not what I want to do. It is a large db... There must be a way to do it by changing the TT templates...
    – Leo
    Jun 25, 2018 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.