none
How to stop the DbContext generator from changing columns names RRS feed

  • Question

  • If a table has column names that are the same as the table name the DbContext Generator suffixes the column name with a "1".

    SQL Server Table definition:

    CREATE TABLE [dbo].[Test](
        [Test] [nchar](10) NOT NULL,
        [ColumnsTwo] [nchar](10) NULL,

    EF Model created:

             <EntitySetMapping Name="Test">
                <EntityTypeMapping TypeName="AdventureWorksModel.Test">
                  <MappingFragment StoreEntitySet="Test">
                    <ScalarProperty Name="ColumnsTwo" ColumnName="ColumnsTwo" />
                    <ScalarProperty Name="Test1" ColumnName="Test" />
                  </MappingFragment>
                </EntityTypeMapping>
              </EntitySetMapping>
            </EntityContainerMapping>

    This causes  SqlQueries to throw the error "The data reader is incompatible with the specified 'AdventureWorksModel.Test'. A member of the type, 'Test1', does not have a corresponding column in the data reader with the same name."

    Why does the DbContext generator modify the column name? The ObjectContext generator left them alone.

    How can we fix this? We have no control over the DB schema.

    Thursday, October 4, 2012 3:33 PM

Answers

  • The EDMX generator in the end generates a class that you use in the rest of your application. So if you have an entity called Test, because of the table name, with a property called Test, because of the column name, then it would need to generate a class that looks something like this:

        class Test
        {
            public string Test { get; set; }
        }

    The problem with that is that the C# compiler will not let you have a class with a member of the same name. If you try to compile the example above you will get an error like this:

    Error 1 'Test': member names cannot be the same as their enclosing type

    Hope that makes sense.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by pmeinl Sunday, October 7, 2012 2:56 PM
    Friday, October 5, 2012 5:40 PM
    Moderator

All replies

  • Hi,

    This is actually by design. The SQL Query method will not make any effort to use your mapping information to map columns to entities. It just uses names.

    You can use aliases in your SQL query to match the column names in the entity.

    There is a Codeplex issue open to change this behavior here: http://entityframework.codeplex.com/workitem/233

    It has some extra information and you can upvote it if you want to see this change in the future.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Thursday, October 4, 2012 8:53 PM
    Moderator
  • Thanks for  your quick reply.

    But why does the EDMX generator destroy the column name at all?


    • Edited by pmeinl Friday, October 5, 2012 8:10 AM removed wron comment about this being a breaking change
    Friday, October 5, 2012 5:12 AM
  • The EDMX generator in the end generates a class that you use in the rest of your application. So if you have an entity called Test, because of the table name, with a property called Test, because of the column name, then it would need to generate a class that looks something like this:

        class Test
        {
            public string Test { get; set; }
        }

    The problem with that is that the C# compiler will not let you have a class with a member of the same name. If you try to compile the example above you will get an error like this:

    Error 1 'Test': member names cannot be the same as their enclosing type

    Hope that makes sense.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    • Marked as answer by pmeinl Sunday, October 7, 2012 2:56 PM
    Friday, October 5, 2012 5:40 PM
    Moderator