none
After mapping the table name can't be the same to column name? RRS feed

  • Question

  • Hi, all

     

    I have a table named "Specializations" with a  column named "Specialization" in SQL Server 2005.

     

    After the mapping with option "/pluralize", LINQ removes the "s" and give me an "Specialization" class but appended "1" to the property name "Specialization1" and when I changed it back it said:

    "An item named "Specialization" already exists. Please choose a different name".

     

    So I can' t have class names same as property names?

     

    Thanks!

     

    Matt

    Friday, July 25, 2008 1:12 AM

Answers

  • You can not have a member name with the same name as the class it belongs to in .NET. e.g.

     

    class Specialization {

       string Specialization;

    }

     

    will produce the error "Error 1 'Specialization': member names cannot be the same as their enclosing type"

     

    This is not specific to LINQ to SQL or the designer but rather a limitation imposed by the .NET Framework. The only option would be to consider alternate naming for the properties such as AssociatedSpecialization.

     

    [)amien

    Tuesday, August 19, 2008 8:25 PM
    Moderator

All replies

  • Also, since I have a foreign key reference, it makes the names more confusing.

     

    I have a "SpecializationRecords" table which has a "Specialization" foreign key referencing "Specialization" primary key in "Specializations" table.

     

    After the mapping, the property for the foreign key has the name "Specialization" which makes the association property for EntityRef<Specialization> can't be named "Specialization" and so it becomes "Spcialization1". I have to change a lot of names in the designer but the underlying private fields still have "1" appended which doesn't matter but makes me uncomfortable and I think I'll go back to my SQL Server 2005 to rename the columns.

     

    P.S. I tried to play tricks with designer, like first to rename the class to "Specialization1" and then the property "Specialization" and then the class "Specialization" since in SSMSE for newsequentialid() it turns out that is GUI's bug. I was happy because the designer didn't complain and I can save it but after I press CTRL+F5, the errors come back...

    ;-( so it's not a GUI bug...

     

    Friday, July 25, 2008 3:01 AM
  • You can not have a member name with the same name as the class it belongs to in .NET. e.g.

     

    class Specialization {

       string Specialization;

    }

     

    will produce the error "Error 1 'Specialization': member names cannot be the same as their enclosing type"

     

    This is not specific to LINQ to SQL or the designer but rather a limitation imposed by the .NET Framework. The only option would be to consider alternate naming for the properties such as AssociatedSpecialization.

     

    [)amien

    Tuesday, August 19, 2008 8:25 PM
    Moderator