I'm having problems saving objects with relation to other objects. It
works fine when I create and save a Customer object but when I try to
create an order object it doesn´t work.Can someone please help me. I'm
using nhibernate 2.1.2.400 and a local SQL Sever database.
This is the error msh:
NHibernate: INSERT INTO Customer (CustomerId) VALUES (@p0);@p0 =
e60f016b-3e73-42e7-b3bd-7b2fabb98b1a
NHibernate: INSERT INTO Order (CustomerId, OrderId) VALUES (@p0,
@p1);@p0 = e60f016b-3e73-42e7-b3bd-7b2fabb98b1a, @p1 =
881d9396-1bb0-4e4e-91f6-918790260357
16:10:45,968 ERROR [ 7] AbstractBatcher [(null)]- Could not execute
command: INSERT INTO Order (CustomerId, OrderId) VALUES (@p0, @p1)
A foreign key value cannot be inserted because a corresponding primary
key value does not exist. [ Foreign key constraint name =
FK4EB6D6485020193 ]
System.Data.SqlServerCe.SqlCeException : A foreign key value cannot be
inserted because a corresponding primary key value does not exist.
[ Foreign key constraint name = FK4EB6D6485020193 ]
These are my classes:
public class Order
{
public virtual Guid OrderId { get; set; }
public virtual Customer Customer { get; set; }
}
public class Customer
{
public virtual Guid CustomerId { get; set; }
public virtual IList<Order> Orders { get; set; }
}
The hbm file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyFirstNHibernate"
namespace="MyFirstNHibernate.Domain"
default-lazy="false">
<class name="MyFirstNHibernate.Domain.Order" table="Order">
<id name="OrderId" column="OrderId">
<generator class="guid" />
</id>
<many-to-one name="Customer"
class="MyFirstNHibernate.Domain.Customer" column="CustomerId"/>
</class>
<class name="MyFirstNHibernate.Domain.Customer" table="Customer">
<id name="CustomerId" column="CustomerId">
<generator class="guid" />
</id>
<bag name="Orders" cascade="all" inverse="true" >
<key column="OrderId"/>
<one-to-many class="MyFirstNHibernate.Domain.Order" />
</bag>
</class>
</hibernate-mapping>
My test method:
private void CreateTestData()
{
using (ISession session = _sessionFactory.OpenSession())
using (ITransaction transaction =
session.BeginTransaction())
{
var customer = new Customer();
customer.Orders = new List<Order>();
customer.Orders.Add(new Order { Customer =
customer });
session.SaveOrUpdate(customer);
transaction.Commit();
}
}
/Markus
--
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.