views:

476

answers:

1

I'm having a strange behavior in Linq to Sql...

I have a simple parent | child (one to many) object model. In my app, I create the parent object and populate the children all in memory, and then call my repository's add method to insert both the parent with its children.

Now from my application (winform), all works correctly as expected, but I've set up a small method in my unit test project, just to exercise repositorie's add method (I know is not unit test since I'm hitting the database), and from the test method, it returns a "The INSERT statement conflicted with the FOREIGN KEY constraint" exception.

I've trace both call and their receive the parent object correctly constructed (ie. parent has a list of children, and each children has reference to the parent id and parent object).

My insert method looks like this:

public void AddStartList(StartList list)
{
    using (MyDataContext context = new MyDataContext())
    {
     context.Log = Console.Out;
     context.StartLists.InsertOnSubmit(list);
     context.SubmitChanges();
    }
}

And the output log from linq both look the same, except for the second child (in the case of the test method) it can't associate the parentId with the child

The output log for the one the WORKS is

INSERT INTO [dbo].[StartLists]([Name], [TargetControl], [FirstCarDue], [Status])
VALUES (@p0, @p1, @p2, @p3)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input VarChar (Size = 4; Prec = 0; Scale = 0) [Test]
-- @p1: Input Char (Size = 10; Prec = 0; Scale = 0) [CH0]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:00:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

INSERT INTO [dbo].[StartListEntries]([StartListId], [CarNo], [StartTime], [Order], [Notas])
VALUES (@p0, @p1, @p2, @p3, @p4)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [18]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:00:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

INSERT INTO [dbo].[StartListEntries]([StartListId], [CarNo], [StartTime], [Order], [Notas])
VALUES (@p0, @p1, @p2, @p3, @p4)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [18]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:01:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

and the one that DOES NOT WORK is:

INSERT INTO [dbo].[StartLists]([Name], [TargetControl], [FirstCarDue], [Status])
VALUES (@p0, @p1, @p2, @p3)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input VarChar (Size = 4; Prec = 0; Scale = 0) [Test]
-- @p1: Input Char (Size = 10; Prec = 0; Scale = 0) [CH0]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:00:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

INSERT INTO [dbo].[StartListEntries]([StartListId], [CarNo], [StartTime], [Order], [Notas])
VALUES (@p0, @p1, @p2, @p3, @p4)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [19]
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:00:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

INSERT INTO [dbo].[StartListEntries]([StartListId], [CarNo], [StartTime], [Order], [Notas])
VALUES (@p0, @p1, @p2, @p3, @p4)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [0]   **<<----- THIS ONE SHOULD BE 19!!!**
-- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
-- @p2: Input DateTime (Size = 0; Prec = 0; Scale = 0) [1/1/2009 8:01:00 AM]
-- @p3: Input Int (Size = 0; Prec = 0; Scale = 0) [2]
-- @p4: Input VarChar (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: MappedMetaModel Build: 3.5.30729.1

Any ideas?

PS. Sorry for the long post

A: 

Never mind... I was doing a silly mistake on my test and not associating the second child correctly to its parent StartList

Jaime