2009-06-11 4 views
0

Ich bin ein seltsames Verhalten in Linq zu Sql mit ...LINQ to SQL INSERT-Abfrage funktioniert nicht von Unit-Test-Projekt

ich einen einfachen Elternteil | Kind (eins zu viele) Objektmodell. In meiner App erstelle ich das übergeordnete Objekt und befülle die untergeordneten Elemente im Speicher. Anschließend rufe ich die Add-Methode meines Repositorys auf, um sowohl das übergeordnete Element als auch die untergeordneten Elemente einzufügen.

Jetzt von meiner Anwendung (winform), funktioniert alles korrekt wie erwartet, aber ich habe eine kleine Methode in meinem Unit-Test-Projekt eingerichtet, nur um Repositoria Add-Methode (ich weiß, ist nicht Unit-Test, da ich bin trifft die Datenbank), und von der Testmethode gibt es eine "Die Anweisung INSERT-Anweisung, die mit der FOREIGN KEY-Einschränkung konfliktfrei ist" zurück.

Ich habe sowohl Call-Trace und empfangen das Elternobjekt korrekt erstellt (dh. Eltern hat eine Liste von Kindern, und jedes Kind hat einen Verweis auf die Eltern-ID und Elternobjekt).

Meine insert-Methode sieht wie folgt aus:

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

Und das Ausgabeprotokoll aus Linq beide gleich aussehen, mit Ausnahme für das zweite Kind (im Fall des Testverfahrens) kann es nicht die parentId assoziieren mit dem Kind

das Ausgabeprotokoll für den einen der WORKS

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 

und das ist, das nicht funktioniert ist:

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 

Irgendwelche Ideen?

PS. Sorry für die lange Post

Antwort

0

Macht nichts ... Ich machte einen dummen Fehler auf meinem Test und nicht die zweite Kind korrekt zu seinem Eltern StartList

zuordnen