开发者

Issue with linq2sql with this specific stituation

开发者 https://www.devze.com 2023-01-06 15:36 出处:网络
Software used: Visual studio 2008 professional with services pack 1 Sql Server 2005 Standard Edition (9.00.4266.00)

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 [PRIM开发者_JAVA百科ARY]
    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?


I actually created your database just as you specified and ran exactly this code it locally on my box. I get no such error when I substitute the a real GUID in this line:

   test.tableguid = New Guid("guid from table 1")

Are you sure your GUIDs are in the right format? Are you sure your tables are created exactly like you specified? Double check it... My guess is that if you recreate this sample db from scratch, you won't see this problem.


I believe Linq2sql doesn't like it when you set a foreign key id directly. It prefers you to set the foreign object itself.

test.table_2 = db.Table_2.First(t2 => t2.table2id = 1);
test.tableguid = New Guid("guid from table 1") 
test.table3filler = "a"


ok, it's in fact a bug with .net 3.5 and fixed with .net 4.0

but there is a hotfix, see detail here

everything work like it should after that hotfix is installed

0

精彩评论

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