views:

50

answers:

4

Every LINQ blog I found there seemed around 2 years old, I understand the syntax but need more direction on creating the SQL mapping and context classes.

I just need to use LINQ for 2 SQL tables I have, nothing complicated. Do folks write the SQL mapping classes by hand for such cases or is there a decent tool for this?

Can someone point me in the right direction?

+2  A: 
  1. In your project right-click to open the context menu
  2. Add new item
  3. Linq-to-Sql data classes
  4. Open the created dbml file in the design view
  5. Open the servers view
  6. Connect to your database
  7. Drag-and-drop your tables to the design view of the dbml

and you are ready to go!

Jader Dias
Can you do the same without dbml?
Nevin Mathai
@Rob I never saw Linq-To-SQL without the dbml.
Jader Dias
@Rob There is a project called DbLinq, which provides an alternative to the dbml, but I think it works only with non-MS databases (MySQL, Oracle, PostgreSQL, etc)
Jader Dias
+1  A: 

You just add a "Linq to Sql data classes" project item to your project. Then you open server explorer, choose your database and drag the tables in question on to the design surface and you are done.

klausbyskov
+1  A: 

If you want to avoid using or generating a dbml file (with the editor or not), I believe you can use SqlMetal to generate a set of code files from a database.

More info here: http://msdn.microsoft.com/en-us/library/bb386987.aspx

Example:

Generate source code from SQL metadata directly:

sqlmetal /server:myserver /database:northwind /namespace:nwind /code:nwind.cs /language:csharp

ChronoPositron
A: 

If you are using a compact sql database look at SqlMetal Builder. This is a gui driven program to generate the dbml file. This is then added to the project. I have found this tool to give me the best results.

Mark W