I'm using Entity Developer to generate business classes and mapping from a DB in c#.
I've used Entity Framework before and it looks like the pilosophy in NHibernate is quite different.
For instance my problem is that classes generated by Entity Developer expose the relations between different entities but do nothing to keep these relations in sync.
Let's take the simple Parent/Child example. I have created two Parent and Child tables in a DB, and Child table has a ParentId column wich is a foreign key to the Parent table.
Entity Developer generated Parent and Child classes, with a Parent property in the Child class, and a collection of Child in the Parent class.
Now, this code generates the exception because the Child instance didn't get its Parent property set :
Code: Select all
class Program
{
static void Main(string[] args)
{
Parent parent = new Parent();
Child child = new Child();
parent.Children.Add(child);
if (child.Parent == null)
{
throw new Exception("The parent in the Child entity was not set");
}
}
}
I wonder if there is a way to generate the same behaviour and use NHibernate as a mapper ? Is this a limitation of Entity Developer ? Is there another tool that can generate entity classes capable of maintaining these relations in sync ? Or did i configure Entity Developer wrong ?
Thanks in advance.
EDIT
Here are the mappings :
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-cascade="save-update" assembly="ConsoleApplication6" namespace="ConsoleApplication6" xmlns="urn:nhibernate-mapping-2.2">
<class name="Child" table="Child" schema="dbo">
<id name="Id" type="Int32">
<column name="Id" not-null="true" precision="10" scale="0" sql-type="int" />
<generator class="identity" />
</id>
<many-to-one name="Parent" class="Parent">
<column name="ParentId" not-null="true" precision="10" scale="0" sql-type="int" />
</many-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping default-cascade="save-update" assembly="ConsoleApplication6" namespace="ConsoleApplication6" xmlns="urn:nhibernate-mapping-2.2">
<class name="Parent" table="Parent" schema="dbo">
<id name="Id" type="Int32">
<column name="Id" not-null="true" precision="10" scale="0" sql-type="int" />
<generator class="identity" />
</id>
<bag name="Children" inverse="true" generic="true">
<key>
<column name="ParentId" />
</key>
<one-to-many class="Child" />
</bag>
</class>
</hibernate-mapping>