Software used:
Visual studio 2008 professional with services pack 1
Sql Server 2005 Standard Edition (9.00.4266.00)
Windows XP SP3
I have these 3 tables:
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_2](
[table2id] [int] IDENTITY(1,1) NOT NULL,
[table2filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_2] PRIMARY KEY CLUSTERED
(
[table2id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Table_1](
[table1id] [int] IDENTITY(1,1) NOT NULL,
[table1guid] [uniqueidentifier] NOT NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[table1id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_Table_1] ON [dbo].[Table_1]
(
[table1guid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table_3](
[tableguid] [uniqueidentifier] NOT NULL,
[table2id] [int] NOT NULL,
[table3filler] [varchar](max) NULL,
CONSTRAINT [PK_Table_3] PRIMARY KEY CLUSTERED
(
[tableguid] ASC,
[table2id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_1] FOREIGN KEY([tableguid])
REFERENCES [dbo].[Table_1] ([table1guid])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_1]
GO
ALTER TABLE [dbo].[Table_3] WITH CHECK ADD CONSTRAINT [FK_Table_3_Table_2] FOREIGN KEY([table2id])
REFERENCES [dbo].[Table_2] ([table2id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Table_3] CHECK CONSTRAINT [FK_Table_3_Table_2]
GO
INSERT INTO [dbo].[Table_2]
([table2filler])
VALUES
('test')
print 'table2id:'
print scope_identity()
GO
declare @guid uniqueidentifier
set @guid=newid()
print 'table1guid:'
print @guid
INSERT INTO [dbo].[Table_1]
([table1guid])
VALUES
(@guid)
GO
now open a new web apps project, create a new dbml and drag&drop these 3 tables
now just put that code in a webpage codebehind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim test As New Table_3
Dim db As New DataClasses1DataContext
test.table2id = 1
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"
db.Table_3s.InsertOnSubmit(test)
db.SubmitChanges()
End Sub
and run it
you will get an invalid cast error
only way so far for me to be able to run that code is to remove the link between the table inside the DBML
is there a way to do that insert without removing the link between the tables?