3
votes

Ok, this may be a double-question. I have a class User, which contains a property Location of type UserLocation. At first, I was mapping both the User class and the UserLocation class and storing them both in the db, but that didn't make a lot of sense to me, since the only purpose for UserLocation is to be a part of User. Anyway, now, my mapping looks like this:

public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Id(x => x.Id).Access.ReadOnlyPropertyThroughLowerCaseField().GeneratedBy.Identity();
            Map(x => x.Location.Address);
            Map(x => x.Location.City);
            Map(x => x.Location.State);
            Map(x => x.Location.ZipCode);
        }
    }

But I get the error "Could not find getter for UserLocation.Address". I also get a bunch of casting errors because it seems NHibernate is still generating a computed class for UserLocation. So I guess the question is what is the best way to reference a non-mapped custom class from inside a mapped one.

Oh, just to add, I'm pretty sure that UserLocation isn't being mapped anywhere. I even tried taking the reference to it out of the User mapping, and I still get casting errors trying to convert the computed class to the real one. I cannot possibly understand why there would even be a computed type at this point..

1

1 Answers

9
votes

What you have there is a component. Components are mapped separately in NHibernate.

Your Fluent NHibernate mapping should look like this:

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id).Access.ReadOnlyPropertyThroughLowerCaseField()
            .GeneratedBy.Identity();

        Component(x => x.Location,
            loc =>
            {
                loc.Map(x => x.Address);
                loc.Map(x => x.City);
                loc.Map(x => x.State);
                loc.Map(x => x.ZipCode); 
            });
    }
}