SQL Metal and Linq - Refreshing Schema Warnings

So it turns out the simplest way to update your dbml schema is to just delete the existing files and recreate them with SQL Metal. Then, when you include the updated dbml file in your VS 2008 project you will get new dbml.layout and .designer.cs files. You'll of course have to reset your layout if you've modified that. If you do this, you're safe. If you need that layout, continue reading.

Just regenerating the file with SQL Metal will overwrite the dbml file but it will not touch the other two files. This means your layout will be ok but you're not out of the woods. If you have changed the name of an object in the underlying structure, your layout will remove that object from the layout and place the newly named object in the default position (usually top left). This object will NOT appear in your intellisense because the .designer.cs file still has the old name. Note that code referencing the old object name will still compile and run but will throw a SQL object not found exception when run.

Error Code: -2146232060
Error Message "Invalid object name 'your object name'."
Error Number: 208

woopsie

So you still want to refresh the schema and keep you designer layout? You have an easy option:

  1. Exclude the dbml file from your project (automatically excludes the other two files).
  2. Delete the .designer.cs file.
  3. Rerun your SQLMetal command.
  4. Re-include the dbml file.

Now the .designer.cs file is regenerated from the dbml and all intellisense and compile-time checking will work. The .cs file contains all the appropriately named items. You also still have your original designer layout but any new objects are placed in the default location and old objects have been removed. 

Now if you're modifying your dbml by adding associations etc., you are going to need a compare tool. I use ScooterSoftware's Beyond Compare. Simple, cheap and reliable. Generate your dbml file to a different location and run the compare utility to "import" all your modifications to the new file. Then perform the steps listed above.

JJ

No Comments