0

how can I override ToStringmethod in a POCO object used as NHibernate model entity? To override object.ToString() you should declare something like

public override string ToString()
    {
        return "my string here";
    }

But NHibernate wants everything inside the POCO mapped objects to be virtual... and you can't declare a method both virtual ad override.

Any suggestion?

themarcuz
  • 2,573
  • 6
  • 36
  • 53

3 Answers3

4

The fact that you are overriding the method means that it's virtual, there should be no problem.

DanP
  • 6,310
  • 4
  • 40
  • 68
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
  • true. I don't know what was going, but I had an NHibernate error message saying that the method must be declared virtual... now I tried again and the message disappered... probably there was something else wrong in my code – themarcuz Feb 03 '12 at 13:20
1

What is the reason to override ToString? Is it for debugging purposes? If so, while this doesn't actually answer your question, you could look at Debugger Attributes

Fen
  • 933
  • 5
  • 13
-1

My C# and NHibernate are both a bit rusty but I believe

public virtual string ToString()
{
    return "my string here";
}

is perfectly valid code.

The generated proxy should then use the provided implementation of ToString

urvaksh
  • 1
  • 1