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!