locked
Problem with Guids RRS feed

  • Question

  • User4068249 posted

    I'm having a weird problem that I think is related to the use of primary keys that are GUID columns.  I'm using VS2010, C# and EF4 and SQL Server.

    I'm trying to override the ToString() method on my custom partial class entity in order to change how the entity is shown when retrieved as a foreign key

    (typical FirstName + LastName scenario).  However, when I type 'return FirstName.ToString() + " " + LastName.ToString();'

    I get errors saying that 'FirstName (and LastName) does not exist in the current context'.

    The only difference that I can see between examples that work and examples that don't is that the ones that don't have GUID primary keys,

    while the others have int IDENTITY primary keys.

    How can I get this override to work with these entities?

     

    Friday, September 20, 2013 2:27 PM

Answers

  • User4068249 posted

    Thanks for all of your suggestions. However, I was able to resolve the issue myself.

    My database had some tables that were designed for a legacy app and were named in the plural form,

    while the newer ones were named in singular form. I wrote a program to generate the code for my

    partial classes which used the tablenames from the DB to create the partial classes for the entities.

    So, for the plural named tables,  my partial classes wound up being replications of the entity COLLECTIONS

    rather than the entities themselves. Once I corrected this, it worked fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 11:02 AM

All replies

  • User753101303 posted

    Hi,

    This message means that the class doesn't have a FirstName & LastName properties. More likely this is because your partial class definition doesn't match the class you are trying to "split" (in particular double check both definition are in the same namespace).

    Saturday, September 21, 2013 12:52 PM
  • User-330204900 posted

    See Rick Anderson's post here Improving the FK field display: Showing two fields in Foreign Key columns the PK should not have any effect here, the only issue I have had with GUId is on Insert and I had to add a constructor to set the GUID.

    Saturday, September 21, 2013 3:12 PM
  • User4068249 posted

    Thanks for your response. However, my partial class does have both of those properties

    (in fact, none of the columns/properties for that entity appear via Intellisense)

    and they are in the same namespace. There is only one namespace being used.

    The table with the FirstName field is not being scaffolded, but is used for foreign key lookups.

    In the child tables that are scaffolded, the dropdown control displays the records but does not show the

    combined fields as I would like. Any other ideas would be appreciated.

     

    Monday, September 23, 2013 10:24 AM
  • User753101303 posted

    My other idea is the same ;-). Try :

    class DataTest
            {
                public string Name { get; set; }
                 string ToString()
                {
                    return AnotherName;
                }
            }

    Do you have the same message ? Basically it does mean that you are trying to use a symbol which is not defined in this scope. What if you are typing "this." to see if "intellisense" does see all the members of the same class ? If it works, could be a typo or whatever. It is still doesn't work more likely you do have a partial class issue.

    Or sow some code (perhaps not the whole code but strip down to the minimal amount of code needed to show the problem including the namespaces definition).

     

    Edit : I noticed "in fact, none of the columns/properties for that entity appear via Intellisense". So more likely this is not correct. For example just have to String() to return some hardcoded string and see what happens if you call this.

    Monday, September 23, 2013 10:40 AM
  • User-330204900 posted

    The table with the FirstName field is not being scaffolded, but is used for foreign key lookups.
    from this I am assuming that this column forms part of a PK or FK? to display this you would use the [Display(AutogenerateField=true)] on that property.

    Tuesday, September 24, 2013 6:48 AM
  • User4068249 posted

    Thanks for all of your suggestions. However, I was able to resolve the issue myself.

    My database had some tables that were designed for a legacy app and were named in the plural form,

    while the newer ones were named in singular form. I wrote a program to generate the code for my

    partial classes which used the tablenames from the DB to create the partial classes for the entities.

    So, for the plural named tables,  my partial classes wound up being replications of the entity COLLECTIONS

    rather than the entities themselves. Once I corrected this, it worked fine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 25, 2013 11:02 AM
  • User753101303 posted

    I was able to resolve the issue myself

    It still seems to be a partial class mismatch definition (i.e. having "Car" in one class definition and "Cars" in the other partial class definition) as I have suggested twice earlier... ;-)

    Wednesday, September 25, 2013 11:08 AM
  • User4068249 posted

    Good point. Sometimes its hard to see the forest for the trees.

    Thanks for your suggestion :)

     

    Wednesday, September 25, 2013 11:21 AM