3

Is there a way in EF4.1 to generate Entity classes that follow custom naming conventions instead of exactly matching the db column names?

We are starting to use EF4.1 for an existing project. We would like to follow our existing camel case conventions for property names on our entities, which our db columns do not follow.

Using DB first, I have columns similar to [first_name] and [last_name] and I'd like to generate model properties in my EDMX like FirstName and LastName.

Update: I do not want to manually update all the names!

2 Answers 2

2

The point of mapping is to manually define new names. If you have some convention which can be always inferred from column name you can build custom tool/script/transformation which will change your EDMX file automatically (it is just XML file and you need to change its CSDL part and MSL part).

Sign up to request clarification or add additional context in comments.

2 Comments

Of course, didn't even think of that, can just write a little post processor.
Very helpful! Thanks! I guess its time to get my custom tool kung fu rolling!
1

Per another answer here I created a custom tool and put the source on GitHub: EdmxUpdater (disclaimer, the UI is horrible).

Basically I took all of the XSD's and generated XML serialization types, then wrote some code to actually perform the name updates. The project is very rough but basically handles Pascal casing and removal of underscores from properties.

It uses the mapping so the name changes are idempotent.

    protected override void UpdateEdmx(TEdmx edmx)
    {
        var scalarProperties = from esm in edmx.Runtime.Mappings.Mapping.EntityContainerMapping.EntitySetMapping
                               from etm in esm.EntityTypeMapping
                               from f in etm.MappingFragment
                               from sp in f.ScalarProperty
                               select new { ScalarProperty = sp, etm.TypeName };

        var mapQuery = from sp in scalarProperties
                       let item = new { sp.TypeName, sp.ScalarProperty.Name, sp.ScalarProperty.ColumnName }
                       group item by sp.TypeName into g
                       select g;

        //build map of new entity property names
        var map = mapQuery.ToDictionary(g => g.Key, g => g.Distinct().ToDictionary(sp => sp.Name, sp => UpdateName(sp.ColumnName)));

        //update map property names:
        foreach (var sp in scalarProperties)
        {
            sp.ScalarProperty.Name = map[sp.TypeName][sp.ScalarProperty.Name];
        }

        //conceptual entities
        foreach(var entity in edmx.Runtime.ConceptualModels.Schema.EntityType)
        {
            var typeName = String.Format("{0}.{1}", edmx.Runtime.ConceptualModels.Schema.Namespace, entity.Name);
            var typeMap = map[typeName];

            //keys
            foreach (var keyRef in entity.Key.PropertyRef)
            {
                keyRef.Name = typeMap[keyRef.Name];
            }

            //conceptual properties
            foreach (var property in entity.Property)
            {
                property.Name = typeMap[property.Name];
            }
        }

        var principalPropertyQuery = from association in edmx.Runtime.ConceptualModels.Schema.Association
                                     let end = association.End.Where(e => e.Role == association.ReferentialConstraint.Principal.Role).Single()
                                     from property in association.ReferentialConstraint.Principal.PropertyRef
                                     select new { TypeName = end.Type, Property = property };

        var dependentPropertyQuery = from association in edmx.Runtime.ConceptualModels.Schema.Association
                                     let end = association.End.Where(e => e.Role == association.ReferentialConstraint.Dependent.Role).Single()
                                     from property in association.ReferentialConstraint.Dependent.PropertyRef
                                     select new { TypeName = end.Type, Property = property };

        foreach (var property in principalPropertyQuery.Union(dependentPropertyQuery))
        {
            property.Property.Name = map[property.TypeName][property.Property.Name];
        }
    }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.