Linq Using Null FK

V

Vaccano

I have an issue with Linq to SQL using a Null FK. Here is the situation.

Two Tables:
TableA
PKColumn UniqueIdentifier
<Other Columns>

TableB
PKColumn int
FKColumn UnqiueIdentifer (can be Null) --This is a FK to table A PKColumn.

I create all of this in my Linq To SQL designer (using normal drag and drop
methods) and then when I try to call:

dataContext.TableB.InsertAllOnSubmit<TableB>(ListOfTableBs);
dataContext.SubmitChanges();

on objects that have the FKColumn set to null I get the following exception:
Specified cast is not valid.

I have checked and double checked that my values that allow nulls are all
correct (the FKColumn is set to allow nulls in both the DB and the designer.)

Note that the insert works fine when the FKColumn value is a valid guid.

I have also run straight SQL and Sprocs to do the insert of the data and
that works too (just to ensure that my data schema was correct.)

I have tried to override the Insert portion of my TableB object in the Linq to
SQL Designer to use a working sproc but I still get the same exception.

The lack of any real info (from the debugger) on what has gone wrong makes
this error even more frustrating. (The call stack says nothing useful.)

Any ideas or fixes would be greatly appreciated.

Vaccano
 
J

\Ji Zhou [MSFT]\

Hello Vaccano,

Thanks for using Microsoft Newsgroup Support Service, my name is Ji Zhou
[MSFT] and I will be working on this issue with you. I try to reproduce
your issue on my side, but with no luck. The following is what I have done.
If my steps has any difference from yours, please feel free to let me know.

1.I created a test database TestDB using SQL Server 2005 which included the
TableA and TableB,

2.The TableA and TableB's schemas were as follows,

TableA:
-----------------------------------------------------
USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableA](
[PKColumn] [uniqueidentifier] NOT NULL,
[TestColumn] [int] NULL,
CONSTRAINT [PK_TableA] PRIMARY KEY CLUSTERED
(
[PKColumn] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
-----------------------------------------------------
TableB:
-----------------------------------------------------
USE [TestDB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TableB](
[PKColumn] [int] NOT NULL,
[FKColumn] [uniqueidentifier] NULL,
CONSTRAINT [PK_TableB] PRIMARY KEY CLUSTERED
(
[PKColumn] 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
ALTER TABLE [dbo].[TableB] WITH CHECK ADD CONSTRAINT [FK_TableB_TableA]
FOREIGN KEY([FKColumn])
REFERENCES [dbo].[TableA] ([PKColumn])
GO
ALTER TABLE [dbo].[TableB] CHECK CONSTRAINT [FK_TableB_TableA]
-----------------------------------------------------

3.Put some dump data into these tables.

4.In Visual Studio 2008, I created a Windows Form Application project. In
the project, I created LINQ to SQL Classes.

5.Connected the TestDB in the Server Explorer and dragged both the TableA
and TableB onto the designer's surface.

6.Afterwards, I used the following codes to insert rows into the TableB and
updated back to the data base, but I never encountered the exception
"Specified cast is not valid". It worked fine.

private void button1_Click(object sender, EventArgs e)
{
DataClassesDataContext dataContext = new DataClassesDataContext();
List<TableB> ListOfTableBs = new List<TableB>();

TableB tb1 = new TableB();
tb1.PKColumn = 11;
tb1.FKColumn = new Guid("3359F5F3-24AD-400f-845B-3EDE149061A0");
TableB tb2 = new TableB();
tb2.PKColumn = 12;
tb2.FKColumn = new Guid("30565AC2-8D2E-4b06-989A-8CE057BCAD8E");
TableB tb3 = new TableB();
tb3.PKColumn = 13;
tb3.FKColumn = null;

ListOfTableBs.Add(tb1);
ListOfTableBs.Add(tb2);
ListOfTableBs.Add(tb3);

dataContext.TableBs.InsertAllOnSubmit<TableB>(ListOfTableBs);
dataContext.SubmitChanges();
}

So, may I suggest you use the above steps in your side to see if you can
still reproduce the error? To better troubleshoot this problem, firstly I
need to reproduce it on my side. So would you like to provide the detailed
reproduce information. The sample SQL database file with pseudo data, as
well as the detailed codes are preferred. If you cannot share that, would
you mind posting the TableA and TableB's SQL statement in the step2, and
the exception's stack trace? Sorry about taking so many questions to you. I
just intend to help on this and that information is needed to narrow down
the problem. Thanks for your understanding!

If there are confidential data in these information, would you like to
share it by sending me emails? You can access me by the address
(e-mail address removed).

Have a nice day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

\Ji Zhou [MSFT]\

Hello Vaccano,

I am writing to check the status of the issue on your side. Could you
please let me know if the issue still persists? If you still need help on
this, please do not hesitate to let me know, and I will be more than happy
to provide future help.

Have a great day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top