views:

1518

answers:

5

Hello. The way I develop may not be correct, any advice welcome.

At the moment I have a WPF application that uses a SQL2008 database. I have a copy of the database on a laptop and on my home machine. My application is versioned using SVN and I am obviously able go from the work laptop to the home machine and update/commit as required to ensure I am using the latest code for the application.

However the database is a different story in that any change I make I create a backup and then transfer the backup to the other machine etc. This way I get the data and the changes made on each system. In order to do this the database connection using a different connectionstring and I change a setting in my app to use a different connection based on my location.

I have now started to use LINQ to SQL and DBML files in my application, and finally getting to the question, I don't know how I can change the connectionstring it uses in code so it will use the correct database in the DBML.

Also, is there a better way to transfer the database so I don't need to do the backups and restores? The only reason why I have not versioned the Schema is because I am not sure how that would handle my data as this is key to my development, ie various environment settings etc are stored in the DB and brought through at runtime.

A: 

You can try Sql Server Merge Replication and use SQL Compact 3.5 as your laptop database and use master as your work/home machine database. However you may do this with only Sql Server Standard Edition.

Other option is , Microsoft Sync Framework.. here..

http://msdn.microsoft.com/en-us/sync/default.aspx

Akash Kava
Akash, it sounds like my current method is easier to maintain though ;). Thanks for your reply.
TravisPUK
+1  A: 

This is such a common problem, and I have never found a minimal and clean solution to it. How to keep all the values and variables and databases and source files in sync between machines?

  • Well SVN works great for the source files.
  • For the database, I TRY to just use one DB if we can get away with it. All the devs point to one machine that hosts the db, then we aren't wasting time with DB setup and merging. If that's not possible, then we usually just end up dumping the database when there is a change and distributing the .bak file around. You can try adding this file to SVN, and it works. you can even have the DB dump to a schedule so that SVN is always getting a new copy. But it's still too much work to keep restoring a db over and over. Perhaps you could hook in some scripting to SVN (we use Tortise for windows) and have a job that would do that automatically. That'd be nice.

  • For the config files - I do ASP.NET so I have web.config, connectionstrings.config, etc, I do one of two things - either just manually copy sections that need to be changed between machines and comment out the part that doesn't need to be used (clunky), or I've at times written ConfigurationSettings helper objects that diagnose a config key to decide what setting to use, based on the current machine name. eg:

Say my current machine is DEV1. The server is SERVER1. I'll have config keys with names like DEV1.connections.sqlserver and SERVER1.connections.sqlserver. In the code I'll use the helper method GetConfig("connections.sqlserver"). GetConfig figures out which key to use based on the current machine name.

Using this method, I don't have to keep remembering to monkey around with the dozen .configs every time I upload to the server or change things. But I DO have to make a duplicate key for every machine that will be running the application, which can get a bit much. For large teams, instead of using machine names, I use group names and have a config key that assigns machine names to a group - with the idea that every machine in the group will have that application set up in an identical fashion - same file paths etc.

Now onto your second question about LINQ - when you create a linq dbml, it will add a connection string to your config. you just have to make sure that you find this connectionstring and copy it into your active application. eg:

I have a solution that has 2 projects: 1 - website 2 - library

I put the dbml into the library project. If I go and look into the App.config of the library project, I'll see the connectionstring that LINQ wants to use. If I copy this connectionstring into the website's connectionstrings.confing file, when I reference the library and run the website, LINQ will be able to see the connectionstring it wants to use.

Lanceomagnifico
Lanceomagnifico, thanks for your comments. I am not sure how your point on the DBML file will allow me to have different connections, ie different database instances. Mainly because the DBML file also contains the actual connectionstring information, ie server name etc.
TravisPUK
The problem with having a single DB is that my home pc does not connect directly to the server that stores a staging DB and the live DB so I need a local copy. Obviously the connection to the local copy is totally different... which creates my problem.
TravisPUK
As to the DBML connectionstring, you *can* code it in a .cs file as part of your DataAccess layer. In the code you can do a lookup for the connectionstring name based on the computer being used - which is useful if you implement a solution similar to the one I outlined above.Here are two different ways to get a dynamic connectionstring working for LINQSee: http://stackoverflow.com/questions/1188962/linq-to-sql-set-connection-string-dynamically-based-on-environment-variablehttp://www.shaneandcasey.com/2009/05/27/linq-to-sql-dynamic-connection-string/
Lanceomagnifico
A: 

You could use red_gate's SQL COmpare and SQLDataCompare to script out changes to the database. You should be in the habit of scripting database changes anyway as that is what you will need to do when it is time to move changes to prod. I would also make sure all database changes are in SVN, we don't make any changes to the database ever without a script in source control.

HLGEM
HLGEM, yeah all of my sql changes get scripted whilst I am making working on it and then I run the scripts into the prod db these are all stored in a separate project in my solution that are versioned in SVN. My biggest issue is not moving the changes into the prod db, but into my dev dbs that I have in several locations that also requires the data for the testing. I don't want to have to rebuild the data everytime I swap machines.
TravisPUK
I guess the SQLDataCompare would probably do that for me though.
TravisPUK
A: 

I ended up just using multiple connection strings and then manually changing the connection on the dbml file whenever I moved locations. However I also have some code in place to programmatically change it based on the project setting for the location.

I haven't really got a good solution to the transferring of the databases and continue to use the backup and restore method.

TravisPUK
+1  A: 

Your Statement: I have now started to use LINQ to SQL and DBML files in my application, and finally getting to the question, I don't know how I can change the connectionstring it uses in code so it will use the correct database in the DBML.

Yes it's possible.

MYDataContext mycontext = new MYDataContext("Your Connection String");

There is a Constructor where you can chage the Connectionstring.

Ronny
Ronny, thanks for your reply. I was under the impression the connection string was stored in the dbml file itself... at least that is where I am making the change, through the properties of the dbml file. The connection strings are currently defined as App Settings with the alternate server information. I then just modify the connection string it uses when I transfer to another PC via the properties drop list.
TravisPUK
Gave it a whirl and indeed this works pretty well. I still need to modify my dbml file connection, but only when I am making changes to it. Thanks Ronny.
TravisPUK
I am changing the answer on this question to this one rather than what I had put in as my own solution. I am still using my code for programmatically changing the connection string based on whether it is using one server over another, but this answer means I don't need to manually change the connection each time.
TravisPUK