views:

353

answers:

5

I have a weird problem that sometimes when I make a change to a Linq object using the O/R designer (usually by adding a field that I've added in the DB), when I save the project, the designer.cs file gets deleted!

Fortunately I have my source control to fall back on; I undelete the file and back out the changes to the csproj file. But this is really annoying, and doesn't appear to have any good reason (the fact that my project contains about 100 objects is not an excuse).

Has anyone else had this trouble? Any idea what could be causing this to happen?

Edit - additional info - my DataContext class is set up with a custom base class. D'ya think this might have something to do with it...?

+4  A: 

I have a similar problem with the DataSet designer, when working on a big DataSet. You don't need to undelete the file with your source control system : just saving your work in the designer should regenerate the .designer.cs file. You can also right-click the file and select "Run custom tool", which has the same effect.

Thomas Levesque
A: 

Custom tool which generates *.designer.cs file should start by Visual Studio every time when you change and save an original O/R designer file. So it may work in that way that it first deletes an old *.designer.cs file before creating a new one. If it does not create a new one, it is a problem in the custom tool.

Nenad
+3  A: 

Have you added a partial class to add functionality (etc) to the generated classes? If so - odd though it sounds, the position of any using statements in your partial class file can actually be a problem that breaks code generation. Try moving them inside the namespace.

No, I am not kidding.

The error message in this case is "The custom tool 'MSLinqToSQLGenerator' failed. Unspecified error". Changing from:

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

to:

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

fixes it. Freaky bug.

Marc Gravell
Yes, I am using partial classes all over the place! I am gobsmacked at the idea that repositioning the "using" statements could make any difference... can you give more detail please?
Shaul
Well, I've given a full example, that I verified on my machine before posting... note that once it has failed you need to tweak the dbml again (rename a property from Foo to Foo2 and back to Foo, for example) before it will work.
Marc Gravell
Also importantly, make sure your "Warnings" are not hidden...
Marc Gravell
Oh - and it only seems to happen for the like-named file... i.e. if you have MyDataClasses.dbml, look at MyDataClasses.cs (the twin to MyDataClasses.designer.cs)
Marc Gravell
see my edit - I misunderstood the original question. I only use partial classes for the data classes - but my data context has a custom base class. Think this could be the source of the problem?
Shaul
A: 

It may be too early to tell, because the behavior is erratic, but it seems to me that if I keep the designer.cs file open in the IDE editor when I make changes on the .dbml file, then it doesn't get removed when I hit "Save".

I've tried that a few times, and it seems to work... No good explanation why that should be, but then the problem is not one that can be subjected to logical scrutiny, either...

Later...

Having tried this out a few times, I can say that this consistently works, so I'm marking this as the answer!

Thanks to all for your help!

Shaul
A: 

I've just encountered the same issue, Marc's suggestion got me past the custom tool error. But the .designer.cs file was still getting deleted.

I noticed that if I change the DBML and close the DBML (it asks to save, and I choose yes), it will delete the designer.cs file. If I don't close the DBML, but instead tell it to build right now, it seems to regenerate the .designer.cs file and compile without issues.

I can then close the DBML and everything is fine.

Very odd interactions. A coworker also encountered similar issues to what Shaul was reporting above.