views:

178

answers:

3

Hello, I'm starting a new C# project that uses a database, but I want it all to be version controlled. I'm thinking about a SQL server database (If I make a mdf file, will I be able to use it in a computer without SQL Server installed?) and LINQ to SQL.

How do you usually start a project like this? Do you create a .sql file to create the database and then create the linq models? Is there a better way?

+4  A: 

It really depends on what you prefer. You are going to get answers which tell you it's better to design your object model first (programmers will tell you that) and then there are those that are going to tell you to design your data model first (the database admins).

In the end, the right way is the way that you feel most comfortable with. If you feel that you are going to do most of the manipulation of the data in code, then it might be good for you to start with the object model. If you feel you will perform the majority of the operations in data, then it would be good for you to start with the database.

In regards to source control, if you want to version everything, then you are going to have to generate a sql file which will have what you need to create your db from scratch. Then, every time you change or update the database schema or data, you should place that file into source control as well. I also recommend a naming convention which incorporates the date and time of the file into the file, to make it easy to figure out what order to apply the files in when working from scratch, or any point in time after that.

And of course, you need to version your source code as well, but that should go without saying.

casperOne
+2  A: 

First question: yes you always need sql server installed on your machine to access the database locally, otherwise you need to setup your server to accept external connections.

Second: no you should not store the database MDF file under source control. As you said, store sql scripts in source control instead.

Third: I recommend looking at an Object Relation Mapping system such FluentNHibernate and NHibernate so that you don't need to manage any of your database in sql other than the actual table create/alter statements and indexes.

Chris Marisic
+2  A: 

If you use an .mdf, your users will need to have some version of SQL Server. For small apps, you may be able to use the (free) SQL Server Express.

As for how to design your solution, this can be difficult, but your desire to have everything in source control is correct. Too often, developers don't put their database code in source control, and this always leads to headaches.

The problem is that version controlling a database can be tricky in Visual Studio. Just putting change scripts in a folder is difficult, because you have to remember to create them as you design the database. Simple things like field renaming, etc. can be tedious, since doing them in the correct order is important. You may want to consider a migrations framework such as RikMigrations as one approach. For larger apps, a Visual Studio Database Project (requires a new install, but recently made available in the 'Team Developer' Sku) will help you version control database change scripts, etc.

Daniel