Search

LINQ to SQL: NullReferenceException when attaching inherited record by CompuBoy

Closed
as By Design Help for as By Design

2
0
Sign in
to vote
Type: Bug
ID: 310798
Opened: 11/16/2007 9:20:29 PM
Access Restriction: Public
0
Workaround(s)
4
User(s) can reproduce this bug
Suppose we have a simple LINQ to SQL model with only one simple entity 'Foo'. If we inherit a non-LINQ-to-SQL-mapped type 'FooEx' from 'Foo' somewhere in our code, make an instance of 'FooEx', and at sometime, try to attach it to the DataContext as a record of table 'Foo', 'Attach' method raises the following exception:

NullReferenceException:

Message: "Object reference not set to an instance of an object."

StackTrace:    at System.Data.Linq.Table`1.Attach(TEntity entity, Boolean asModified)
                     at System.Data.Linq.Table`1.Attach(TEntity entity)
Details (expand)
Product Language
English

Version

Visual Studio 2008 (All Products and Editions)
Operating System
Windows XP Professional
Operating System Language
English
Steps to Reproduce
1- Define a database table Foo(ID:int(PK), Name:nvarchar(50))
2- Create a console application project.
3- Define a LINQ to SQL model and add entity 'Foo' from the created table.
4- Add a new class 'FooEx' to the project, inherit it from 'Foo', and add an extra property of type 'int' named 'ExtraInfo' to it.
5- In 'Main' write the following code:

var db = new MyDataContext();

db.ObjectTrackingEnabled = false;
db.DeferredLoadingEnabled = false;

var records = (from f in db.Foo
select new FooEx()
{
ID = f.ID,
Name = f.Name,
ExtraInfo = 10
}).ToList();

var saveDB = new MyDataContext();
saveDB.Foo.Attach(records[0]);

records[0].Name = "ABC";
saveDB.SubmitChanges();
Actual Results
Upon executing the described project, the following exception is thrown when trying to attach the record:

NullReferenceException:

Message: "Object reference not set to an instance of an object."

StackTrace: at System.Data.Linq.Table`1.Attach(TEntity entity, Boolean asModified)
at System.Data.Linq.Table`1.Attach(TEntity entity)
Expected Results
Either:

- The record be successfully attached to the DataContext and changes made to it should update the record in the database upon call to 'SubmitChanges'.

or

- If attaching an entity of a non-mapped type which is inherited from a mapped LINQ to SQL entity as an instance of the mapped entity is forbidden a more informative exception should be thrown indicating that this action is forbidden.

I think the first behavior seems to be more logical.
TAP Code (if applicable)
 
      You can indicate your satisfaction with how Microsoft handled this issue by completing this quick 3 question survey. [Details]

 

File Attachments
0 attachments
Sign in to post a comment.
Posted by Andrei Rinea on 2/3/2009 at 6:45 AM
I had a simillar issue and I could fix it very easy. Have a look at http://andreir.wordpress.com/2009/01/30/linq-to-sql-nullreferenceexception-gotcha/
Posted by Microsoft on 2/28/2008 at 10:55 PM
Hi,

Sorry for the delay here.

What you are trying to do is not supported - i.e. attaching a derived class that isn't explicitly mapped through inheritance mapping for instance. Also, notice that you are disabling state tracking - this will mean that you will not be able to submit changes to the database like your code is doing later on after the Attach.

You are right that the exception should be clear and more indicative of the reason for failure. We'll try to fix that in a future release.

Thanks for taking the time to share feedback.

LINQ to SQL Team
Posted by ImNoScrub on 2/26/2008 at 8:57 PM
I'm having this issue as well and it's driving me crazy. I've read dozens of forum posts and none of the 'fixes' or 'hacks' I've seen seem to work. I'm finding LINQ to be more frustrating that it's worth.
Posted by MaxBondarenko on 1/28/2008 at 6:56 AM
the same damn problem :[
Posted by Minnsey on 1/24/2008 at 3:02 AM
Is there likely to be any movement on this issue, since it seems to have been dormant for 2 months?
Posted by CompuBoy on 1/20/2008 at 10:08 PM
This issue still exists on Visual Studio 2008 RTM. It's about 2 months since submission of this issue and there has been no feedback.
Posted by Microsoft on 11/19/2007 at 2:53 AM
Thank you for your feedback. We are currently investigating. The investigation process normally takes 7-14 days. If this issue is urgent, please contact support directly (see http://support.microsoft.com).

If at any time your issue is closed unsatisfactorily, you may edit your issue via Connect and change the status to “Active.”

Thank you,
Visual Studio Product Team
Sign in to post a workaround.