EntityFramework: how to modify edmx to change format of entity names and properties

edmx ef-database-first entity-framework entity-framework-6

Question

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. CLIENT, CLIENT_NAME, D_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?

1
2
6/25/2018 11:10:22 AM

Popular Answer

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.

0
6/25/2018 11:47:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow