views:

129

answers:

3

I'm fairly new to LINQ to SQL, so I could be missing something basic here.

I created a LINQ to SQL layer, generated all the dbml files etc., and created a LINQ query which worked fine. I then made a change to the database, and wanted to get that change reflected in the ORM layer. To do this, I deleted my ORM layer and created a new one (may not be the best way?).

Now my code is not able to see the datacontext object in intellisense and won't compile. I imagine this may be something simple, but I'd also like to understand the bigger picture of how to update the LINQ to SQL ORM layer when the database changes.

A: 

I remember having this issue a bunch. The fix is simple, really. Rebuild your solution! The DataContext and other such classes are generated during a build.

Quite a headache - I wish the DBML tool did this for you when you closed it.

JoshJordan
Rebuilding is insufficient to update the ORM layer. You have to delete the affected table from the designer, and re-add it.
Robert Harvey
He said he did that.
JoshJordan
+2  A: 

Yeah, you don't want to delete your whole DBML file. Open it in the designer, and delete the table that changed. Then drag'n'drop it again from the "Server Explorer" (in the view menu). This will load an updated copy of teh database... Note that if server explorer is already open while you make the change to the SQL schema, you'll need to refresh server explorer so it has the latest versions.

The drawback to this approach is that if you do customizations to the table in the DBML, those need to be redone. This is an infrequent case for me.

Frank Schwieterman
Customizations are unaffected if you put them into a partial class. Your partial class is not regenerated when the DBML is updated.
Robert Harvey
The customizations I was referring to are modifications you'd make within the designer (for example, changing the name of properties, making properties delay-loaded, etc).
Frank Schwieterman
A: 

You can also use SQLMetal to update your DBML classes. Some people even write a script or batch file to automate the process.

Robert Harvey