开发者

Linq to SQL one:one relationship not setting child's PK to parent's identity value

I have two tables: ScheduledEvent and DrawSequence. The ScheduledEvent table has zero or one DrawSequence entries. The PK of the ScheduledEvent is an identity integer which is used in a FK from DrawSequence.

I have created by dbml file, generated the classes and it shows the parent (ScheduledEvent) has an EntitytRef to the child (DrawSequence).

When I create a new parent and set its child to a new DrawSequence and save it, the child's FK field is not set to the parent's newly allocated PK.

I have googled this and found nothing that helps. if you have any ideas, I'd greatly appreciate it.

I've added the change log after this:

INSERT INTO [dbo].[ScheduledEvent]([StartDate], [EndDate], [Ongoing], [Active], [ScheduledEventName], [LastUpdated], [LastUpdatedUser])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input DateTime (Size = 0; Prec = 0; Scale = 0) [11/03/2010 12:00:00 AM]
-- @p1: Input DateTime (Size = 0; Prec = 0; Scale = 0) [11/03/2010 12:00:00 AM]
-- @p2: Input Bit (Size = 0; Prec = 0; Scale = 0) [False]
-- @p3: Input Bit (Size = 0; Prec = 0; Scale = 0) [False]
-- @p4: Input NVarChar (Size = 4; Prec = 0; Scale = 0) [ssss]
-- @p5: Input DateTime (Size = 0; Prec = 0; Scale = 0) [11/03/2010 5:22:04 PM]
-- @p6: Input NVarChar (Size = 10; Prec = 0; Scale = 0) [TODO: fred]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1

INSERT INTO [dbo].[DrawSequence]([ScheduledEventID], [SendEmails], [EmailAdds], [SendPages], [PageNums], [IsInstantDraw], [InstantDrawRunDate])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)

SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [0]
-- @p1: Input Bit (Size = 0; Prec = 0; Scale = 0) [False]
-- @p2: Input NVarChar (Size = 0; Prec = 0; Scale = 0) []
-- @p3: Input Bit (Size = 0; Prec = 0; Scale = 0) [False]
-- @p4: Input NVarChar (Size = 开发者_StackOverflow社区0; Prec = 0; Scale = 0) []
-- @p5: Input Bit (Size = 0; Prec = 0; Scale = 0) [True]
-- @p6: Input DateTime (Size = 0; Prec = 0; Scale = 0) [Null]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1


Maybe your mapping is not correct, did you check that the parents PK is set as Identity and Auto-generated? Normally L2S would SELECT Scope_Identity() after the first one and pass that to the second insert.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜