使用的软件:
Visual Studio 2008专业人员服务包1 Sql Server 2005标准版(9.00.4266.00) Windows XP SP3
我有这3张桌子:
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
现在打开一个新的Web应用程序项目,创建一个新的dbml并拖放这三个表
现在只需将该代码放在网页代码隐藏
中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
并运行它
您将收到无效投射错误
到目前为止我能够运行该代码的唯一方法是删除DBML中表
之间的链接有没有办法在不删除表之间的链接的情况下进行插入?
答案 0 :(得分:0)
我实际上就像你指定的那样创建了你的数据库,并在我的盒子上本地运行了这个代码。当我用这行代替真正的GUID时,我没有得到这样的错误:
test.tableguid = New Guid("guid from table 1")
您确定您的GUID格式正确吗?您确定您的表格是否与您指定的完全相同?仔细检查一下......我的猜测是,如果你从头开始重新创建这个示例数据库,你将不会看到这个问题。
答案 1 :(得分:0)
我认为直接设置外键ID时Linq2sql不喜欢它。它更喜欢你自己设置外来物。
test.table_2 = db.Table_2.First(t2 => t2.table2id = 1);
test.tableguid = New Guid("guid from table 1")
test.table3filler = "a"
答案 2 :(得分:0)