Insert Trouble

Posts   
 
    
Posts: 112
Joined: 09-Aug-2004
# Posted on: 01-Jun-2005 17:31:05   

I have got three tables. I have a many to many relationship (1:m, m:n, n:1) The tables are Customer, CustomerMessage and Message.

I need to insert a record into Message and insert a CustomerMessage for every Customer.

I am getting an INSERT statement conflicted with COLUMN FOREIGN KEY constraint when I do the following.



message = new BL.EntityClasses.message(-1);

message.Message = "...";
...

BL.EntityClasses.CustomerMessage cm;
foreach (BL.EntityClasses.CustomerUnitEntity c in Customers) {
    cm = new BL.EntityClasses.CustomerMessage(-1);
    cm.CustomerID = c.CustomerID;
    message.CustomerMessage.Add(cm);
}

message.Save(true);


What happens in SQL Profiler is the transaction is created, the message entity is inserted then the first customer is inserted. When that happens the exception is thrown and the transaction is rolledback. There are more than one customer so there should be multple CustomerMessage records.

Do you have any ideas what is wrong here?

Thanks!

Posts: 112
Joined: 09-Aug-2004
# Posted on: 01-Jun-2005 17:33:07   

The exception also says The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. But the innerexception is null, is this a bug?

Posts: 112
Joined: 09-Aug-2004
# Posted on: 01-Jun-2005 20:33:43   

Wow... Complete DOH! moment. When I grab the list of Customers I insert a blank element so I can alert the user to choose an item. The first Item was messing everything up.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 01-Jun-2005 20:52:11   

Glad it's solved! simple_smile

Frans Bouma | Lead developer LLBLGen Pro