LINQ to SQL: Visual Studio designer failed to autogenerate .designer.cs data classes

I had a really strange observation today. I opened one of my LINQ to SQL dbml files, made some changes, and then saved it back to have my designer generated data classes updated. But instead of reflecting my changes in the .designer.cs class, Visual studio deleted that designer generated file. I tried several times but every time LINQ designer was deleting my autogenerated data classes. I googled and found an amazing answer by Marc Gravell at this stackoverflow question. The position of using statements was the source of problem!!! I had extended the LINQ generated partial classes and the first statement in that file was “using System”. I removed that and the VS designer was happy again.

So for anyone else that experiences the same problem, try moving your using statements after the namespace declaration. For example, if you have extended your data context or any other data class like this:

using System;
namespace MyNamespace
{
    partial class MyDataContext
    {
        ...
    }
    ...
}

Try rearranging the declarations like this: 

namespace MyNamespace
{
    using System;

    partial class MyDataContext
    {
        ...
    }
    ...
}

Hope this post helps someone else as well.

Advertisements

6 Responses to “LINQ to SQL: Visual Studio designer failed to autogenerate .designer.cs data classes”

  1. Flo Says:

    THANK YOU so much! we’re using SVN and everytime someone changed the model, Visual Studio tried to delete de .designer.cs file which was followed by a warning from the SVN-plugin…

  2. Marc Gravell Says:

    I’ve yet to find anyone who isn’t astounded by this one when they see it ;-p Fortunately, fixed in 4.0

  3. Syed Mehroz Alam Says:

    You are right, Marc. The hack is really surprising. Nice to learn that it is fixed in the next release.

  4. DotNetShoutout Says:

    LINQ to SQL: Visual Studio designer failed to autogenerate .designer.cs data classes « Mehroz’s Experiments…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  5. Amdys Says:

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

    Its been there since 8/12/2008. DOnt know why it marked as closed.

  6. Syed Mehroz Alam Says:

    @Amdys,
    May be because it is going to be fixed in VS2010, as Marc stated.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: