Does your linked object property reference the linked object itself, or the ID value?
If your property is the linked object, your method will simply call the .ToString() method on the linked object, which is by default the class name. If you were logging a Blog from the below example, the Author would be simply the class name like "MyNamespace.Author", or whatever you output when overriding .ToString().
public class Blog
{
public virtual int ID {get;set;}
public virtual string Name {get;set;}
public virtual Author Author {get;set;}
// more properties
}
public class Author
{
public virtual int ID {get;set;}
public virtual string Name {get;set;}
public virtual DateTime Birthday {get;set;}
}
// property design as used with XML mappings
You have multiple options:
- Add another property with the Foreign ID, such as "int AuthorID", and map it to the FK column
- Use a .ToString() implementation which allows you to identify the related object (human readable only).
- Get the object's ID, like "blogToLog.Author.ID". This has some downsides: you need a special case in your logging routine, logging may call lazy load on the related object (meaning: load from DB only for logging), if not already loaded.
- Create an interface with an ID property, let all your persistent classes implement it, and if your property is a class type, cast into it and output the ID.