Hi NH users,

I'm evaluating NH 3.2 and I'm trying to configure OneToOne class
mapping in code.

I'm using Microsoft's AdventureWorks database. There is a 1:1 Employee
-> Contact relationship (Employee table has a FK column ContactId).

I wrote something like this:

public class Employee
{
...
public virtual Contact Contact { get; set; }
...
}

And the mapping:

public class EmployeeMap : ClassMapping<Employee>
{
        public EmployeeMap()
        {
            Table("HumanResources.Employee");
            Id(x => x.EmployeeId, map =>
map.Generator(Generators.Identity));
            OneToOne(x => x.Contact, map => map.Cascade(Cascade.All));
            ...
        }
}

But when I try to persist Employee I get SQL exception saying that
ContactId in table Employee cannot be null. By looking at the logged
SQL statements I see that Contact entity was not persisted (despite
having cascade all set). NH 3.2 tries to persist only Employee class.

When I persist Contact entity explicitly, record is inserted:

repository.Save(employee.Contact);

and later on when I try to persist employee:

repository.Save(employee);

it still throws an error about null ContactId column.

The mapping is wrong, that is obvious but what exactly is wrong with
it? Contact entity has Id(x => x.ContactId) configured.

I cannot find any good example showing class mapping, google returns
me XML one-to-one mappings which I don't want to use.

Any help most welcomed :)

thanks,
Łukasz

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to