Oracle: Can't insert new entity with one-to-one relationship
Posted: Mon 04 Apr 2011 08:12
I am having trouble with a LinqConnect for Oracle data context: I can't insert a new entity together with a new associated entity when the relationship is one-to-one.
I have created a simple Visual Studio solution that reproduces the problem. It contains a LinqConnect data context with just three tables:
But this does not work. I get the following error: "integrity constraint (FK_TEST_Card_TEST_Customers_0) violated - parent key not found".
If I try to set the Card property to an already existing customer record, it works fine. Also, there is no problem when the relationship is one-to-many. Thus the following works perfectly:
Another less important issue is that if I want to add a card to an already existing customer, I need to explicitly set the parent key field (IdCustomer), otherwise the primary key field of the customer (Id) is set to zero and the SubmitChanges invokation fails:
So, is this a bug in LinqConnect, or I am just doing something wrong?
The Visual Studio 2010 project is here in case someone wants to take a look, just change the connection string and create the tables in some Oracle server:
http://dl.dropbox.com/u/17125937/DevArt ... veTest.zip
Thank you!
I have created a simple Visual Studio solution that reproduces the problem. It contains a LinqConnect data context with just three tables:
- Customers
Cards, related to Customers with a one-to-one relationship
Orders,related to Customers with a one-to-many relationship
Code: Select all
var dataContext = new CustomersDataContext();
var customer = new Customer()
{
Name = "Customer Smith"
};
customer.Card = new Card()
{
Number = 1234
};
dataContext.Customers.InsertOnSubmit(customer);
dataContext.SubmitChanges();
If I try to set the Card property to an already existing customer record, it works fine. Also, there is no problem when the relationship is one-to-many. Thus the following works perfectly:
Code: Select all
var dataContext = new CustomersDataContext();
var customer = new Customer()
{
Name = "Customer Smith",
};
customer.Orders.Add(new Order()
{
Date = DateTime.Now
});
customer.Orders.Add(new Order()
{
Date = DateTime.Now
});
dataContext.Customers.InsertOnSubmit(customer);
dataContext.SubmitChanges();
Code: Select all
var dataContext = new CustomersDataContext();
var customer = new Customer()
{
Name = "Customer Smith",
};
dataContext.Customers.InsertOnSubmit(customer);
dataContext.SubmitChanges();
customer.Card = new Card()
{
IdCustomer = customer.Id, //FAIL without this (customer.Id is set to zero)
Number = 1234
};
dataContext.SubmitChanges();
The Visual Studio 2010 project is here in case someone wants to take a look, just change the connection string and create the tables in some Oracle server:
http://dl.dropbox.com/u/17125937/DevArt ... veTest.zip
Thank you!