views:

49

answers:

3

Hi,

I am new to LINQ to SQL.... I need to add a new column to an existing table and I updated the dbml to include this new field. Then in my C# code, I query the database and accessing this new field. Everything is fine with the new database; however, if I load back a previous database without this new field, my program would crash when it's accessing the database (obviously because of this new field). How do I make it, either the database or my C# code to support backward compatibility?

Here's my code snip

I added a field email to Customer table and also add it to the DataContext.dbml, below is the c# code

DataContext ctx = new DataConext ();
var cusList = ctx.Customer;
foreach (var c in cusList)
{
.
.
.
//access the new field
if (c.email != null)
   displayEmail (email);

.
.
.
}

When I ran through debugger, it's crashing at the very first foreach loop if I am using an older version database without the new email field.

Thanks.

+1  A: 

Make sure you upgrade old database. That's what updates are made for.

I don't think there's a better option. But i might be wrong.

Arnis L.
sorry, new to this linq to sql database thing...how would you programmatically update the database?
queandans
Through regular Sql scripts. Easiest way - open mssql table designer, add to it column and script changes. Then call that piece of sql from your app that updates database.Actually - I didn't provide a real answer to your question. All i wanted to say - consider NOT to support database that's actually 2 databases with different structures. That will be maintenance nightmare.
Arnis L.
+1  A: 

Should be a code-land fix. Make your code check for the existance of the column, and use different queries on each case.

Tordek
I added a code sample in my question. The problem occurs in the ctx.Customer because the table has new field. How do I make code check for existence of the new column? can you provide an example? thanks
queandans
The original poster is using LTS as his OR/M, and not coding his data access by hand. LTS does its data access through the DataContext; he is not writing any queries himself that he could switch between.
Funka
A: 

I agree with Arnis L.: Upgrade your old database. LTS is going to want to look for that column called email in your table, and will complain if it can't find it. I could suggest a workaround that would entail using a Stored Procedure, but you'd need to update your old database to use this stored proc, so it's not a very helpful suggestion :-)

How to update your old database? This is the old-school way:

ALTER TABLE Customers
ADD Email VARCHAR(130) NULL

You could execute this manually against the older database through Query Analyzer, for example. See here for full docs on ALTER TABLE: http://msdn.microsoft.com/en-us/library/aa275462%28SQL.80%29.aspx

If you are working on a team with very strict procedures for deployment from development to production systems, you would already be writing your "change scripts" to the database in this same fashion. However, if you are developing through Enterprise Manager, it might seem counter-productive to have to do the same work, a second time, just to keep old database schemas in sync with the latest schema.

For a friendlier, more "gooey" approach to this latter style of development, I myself can't recommend enough the usage of something like the very excellent Red Gate SQL Compare tools to help you keep multiple SQL Server databases in sync. (There are other 3rd party utilities out there that supposedly can roughly the same thing, and that might even be a little cheaper, but I haven't looked much further into them.)

Best of luck!
-Mike

Funka