代码之家  ›  专栏  ›  技术社区  ›  Fredou

具有此特定结构的linq2sql的问题

  •  0
  • Fredou  · 技术社区  · 14 年前

    使用的软件:


    Sql Server 2005标准版(9.00.4266.00) Windows XP SP3操作系统

    我有三张桌子:

        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
    

    现在把代码放在一个网页上

    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内部

    3 回复  |  直到 14 年前
        1
  •  0
  •   Dave Markle    14 年前

    实际上,我创建了您的数据库,就像您指定的那样,并在我的框中本地运行了这段代码。当我在这行中替换一个真正的GUID时,我没有得到这样的错误:

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

    你确定你的guid格式正确吗?您确定表的创建方式与您指定的完全相同吗?再检查一遍。。。我的猜测是,如果您从头开始重新创建这个示例db,您将不会看到这个问题。

        2
  •  0
  •   James Curran    14 年前

    我相信Linq2sql不喜欢直接设置外键id。它更希望您设置外部对象本身。

    test.table_2 = db.Table_2.First(t2 => t2.table2id = 1);
    test.tableguid = New Guid("guid from table 1") 
    test.table3filler = "a"
    
        3
  •  0
  •   Fredou    14 年前

    好吧,这实际上是.NET3.5的一个bug,用.NET4.0修复了

    see detail here

    安装热修复程序后,一切都正常工作