tags:

views:

202

answers:

3

I have extensive experience working in ASP.NET and Sql Server but I am new to Linq. I have just inherited a project that was created using Linq.

Unfortunately the last developer knew nothing of efficiency and was storing images in the database in a truly terrible way. I have modified the code so that it no longer uses the column that stored the image. Now I want to completely delete that column from the database to keep the Linq queries from wasting time and resources pulling in these huge files.

I searched my project for every reference to the column and removed it, then deleted the column from the database (don't worry, I have plenty of backups of everything). When I did this I began to get error messages about an invalid column name for the column I deleted.

So my question is, how the heck do you modify the structure of a table when using Linq?

+1  A: 

You need to be sure to remove the column from the DBML itself. Just view the DBML in the designer and delete the appropriate column. You would not get any error at compile time since it does not check to see if the DBML actually matches up with the database during compiliation.

David
I had already removed every reference to the column in the DBML, no luck.
Justin C
Can you show the line of code causing the error?
David
Here it is, but I don't think posting any code will help. If I commend this code out (as well as the rest of the if statement) the same error appears the next time I reference my object "tblPhotos" which is an EntitySet of "tblPhoto" which is the name of my table that I removed the column from. if(row.tblPhotos.Count==0)
Justin C
+1  A: 

Just delete the table from the Linq-Sql designer, then add it again.

mdresser
Sorry, tried that and it doesn't work.
Justin C
Hmm strange. The Linq-Sql designer does go a bit 'wibbly' sometimes in which case restarting VisualStudio can fix the problem.
mdresser
Got my hopes up mdresser. I had not restarted Visual Studio. I just did though and it didn't change anything, same error message.
Justin C
A: 

If you removed the file from the dbml file, and it didn't pick up on the change, go ahead and right-click the dbml file and choose "Run Custom Tool". Note that if you look at the file's properties (right-click on the entry in solution explorer) you should see the custom tool listed as "MSLinqToSQLGenerator".

Worst case: If you expand the file, and look at the "dbmlfilename.designer.cs" file you should be able to find the field/column name in question. Go ahead and delete it from that file. (one property (with attribute and getter/setter) and one field with the same name (starting with an _ character).

It's the attribute on the property in the designer file that causes the runtime exception.

Eric Falsken
Eric, your "worst case" is what I have tried. I removed every reference to the column in the dbml.As for the "Run Custom Tool", I don't see that at all when I right-click. I also don't see anywhere where a custom tool is listing, MSLinqToSQLGenerator or anything else. Could I be missing any software? I am using Visual Studio 2008 Pro.
Justin C
If you're not seeing "Run Custom Tool", it sounds like something's wrong with your project configuration. This probably also means that your .dbml file isn't getting compiled when you build your project, and you're still dealing with an older compiled version of your LINQ model.Under the "File Properties" of your .dbml file in Visual Studio, "Custom Tool" should be set to MSLinqToSQLGenerator. If this is blank, then (1) you won't have the "Run custom tool..." option on the right-click menu, and (2) rebuilding your solution won't update your model because the DBML file isn't getting compiled.
Dylan Beattie
Dylan, I think you are correct that the DBML file is not getting compiled into the project and instead the project is using an older version of the compiled DBML.When I select the DBML in Visual Studio there is no "Custom Tool" option in the file properties, only "File name" and "Full Path".So, how do I get my project to use the DBML file I have instead of the compiled one?
Justin C
Try editing the project file directly, like so:Add another .DBML ("Linq to SQL classes") file to your project. Then right-click the project, click "Unload Project". The project should appear "greyed-out" in Solution Explorer. Now right-click it again, and you should have an option to "Edit myproject.csproj"You should be able to see both the references to your .DBML file, and to the new .DBML file you've just added. Make sure the syntax for your existing DBML file matches the new one - and check carefully, there may be 2-3 different references to each .dbml file in the .csproj file.
Dylan Beattie