Sunday, October 12, 2008

Linq-to-SQL and Derived Classes

LINQ is a nice new data technology that really provides alot of functionality. LINQ-to-SQL is a version of it that talks to a SQL Server database and will generate classes for you to use. I had a small problem that took a while to figure out that I wanted to share.

I had a class generated from the database called User. From there I had a derived class named ProtectedUser that has some things set in it automatically. When I created a ProtectedUser object when I went to save off the object to the database a null exception was thrown. This was confusing since there was nothing wrong with the object and nothing was null.

Looking around online I found this article that describes the problem:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=310798

After changing my code to create a new User object and save that off instead things worked perfectly fine. This was hard to find and took weeks of working on it on and off to find it. I wanted to make sure to share this with others to save them the time.

1 comment:

Liam S said...

Good reading yyour post