views:

556

answers:

4

Learning C# and learing Linq now. have lots of qestions about it. Basically I need a step by step tutorial.

  1. I suppose the dbml file is the configuration file of the database. I double click it and VS will open it with kind of design diagram. I can create/delete/modify table here? I can use add new item to add the Linq to SQL Classes to get a dbml file?

  2. what's next? generate tables in database? generate sql script? generate cs files? when? how?

+6  A: 

The DBML file is not related to the database server at all. It's a completely client side thing. It's essentially a set of information about your tables in the database and how you're going to map them to .NET objects.

Of course, you can drag a table from a database and have Visual Studio infer some information for you automatically, but changing the file will not affect the database. You can create a DBML file from scratch without any database too.

Internally, the DBML file is simply an XML file that's fed into a custom tool by Visual Studio and generates .cs files representing the LINQ object model for your database from it.

Mehrdad Afshari
is there a way to bypass the dbml file for .cs file creation? my coworker has this command: sqlmetal /server:dwasilew\sqlexpress /database:Ande /code:Ande.cs I guess the Ande.cs file is created in this command. there is no .dbml mentioned.
5YrsLaterDBA
@5Yrs: `SQLMetal` is the tool that (1) can generate `dbml` from a database (2) can generate code out of `dbml` (3) can streamline the two said processes and output source file directly from a database. Visual Studio essentially does the same thing with a GUI. You can see the different command line arguments for SQLMetal here: http://msdn.microsoft.com/en-us/library/bb386987.aspx
Mehrdad Afshari
+2  A: 

The DBML file is mapping that defines your classes based on your database schema. Yes, it defines your (default) connection string, but it doesn't "configure" your database at all.

Linq to Sql uses a database-first approach where you have the database and model your classes after the DB schema. By dragging & dropping the table onto there, you'll be automating the creation of the classes so you don't have to type them out. You can change property names etc from there and the mapping between the property and its correct database column name will remain intact.

statichippo
A: 

Yes, the DBML file is created when you add a Linq to SQL class. In the designer (what you see when you double click the DBML file) you can drag tables (from the server explorer) onto it. You can then reference these tables in your code. There are more than a few getting started tutorials out there:

Check this SO question for details:

http://stackoverflow.com/questions/481244/can-anyone-recommend-a-good-tutorial-for-learning-linq2sql

Loki Stormbringer
A: 

http://msdn.microsoft.com/en-us/library/bb386940.aspx

Be aware of SqlMetal

PK :-)

Paul Kohler