I'm mapping a legacy database and previously users were not required to select a country when they signed up and later added it.
So there are some users who have a CountryID that is NULL. So when my many to one mapping (ByCode) tries to load up the corresponding country details, it finds none, so the user's "country" object is null.
When I try to display user.Country.Name for example, I get a object reference not found.
I could hack this by making a country with an id of -1 called Undisclosed and then change all the null country ids to -1, but that's cheating to solve a common problem I would think.
So my question is how can I map so that if the CountryID is not found, that user gets a new Country object.
My mapping in the user object is:
ManyToOne<Country>(x => x.Country, m =>
{
m.Column("CountryID");
m.Lazy(LazyRelation.NoLazy);
m.NotNullable(false);
m.Fetch(FetchKind.Join);
});
My CountryMap is pretty basic...
public CountryMap()
{
Id(p => p.ID, m => m.Generator(Generators.Identity));
Property(p => p.Code);
Property(p => p.Name);
}
Coding too late... brain freeze...
I simply modified the Country property on the User POCO to return a new Country() if the country was NULL.
Hope this helps someone, but if admins delete the question I won't be offended. :) :) :)