views:

36

answers:

3

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?

A: 

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.

Dave Markle
I updated my sql script to automatically insert new row and return the key, I still get the invalid cast error. Only way for me to do the insert is to remove the link inside the dbml file between every tables
Fredou
see my own answer, it's in fact a bug
Fredou
ah, i'd tested it in 4.0...
Dave Markle
A: 

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"
James Curran
see my own answer, it's in fact a bug
Fredou
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

Fredou