Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# where does the dbml file come from?

I am currently learning C# and LINQ. I have lots of questions about them. Basically, I need a step by step tutorial.

  1. I suppose the dbml file is the configuration file of the database. If I double click the dbml file VS will open it in a design diagram. Can I create/delete/modify tables 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?

like image 206
5YrsLaterDBA Avatar asked Mar 22 '10 21:03

5YrsLaterDBA


4 Answers

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.

like image 51
mmx Avatar answered Sep 25 '22 18:09

mmx


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.

like image 36
hackerhasid Avatar answered Sep 23 '22 18:09

hackerhasid


this link learn how to create dbml File in your Project :

http://dotnetlearners.com/linq/linq-to-sql-creating-dbml-file

Implement LINQ to SQL first we have to create the DBML file, the DBML file contains the C# source code which allow us to write the LINQ Queries to SQL. Here is the step by step process to generate dbml file.

Right click on the project (or project folder) and select the option Add New Item.

add-new-item enter image description here Select LINQ to SQL Classes Template and Give Name as MyDB.dbml.

enter image description here linq-to-sql-classess

Click on Yes when the below confirmation displayed. enter image description here

dialog

MyDB.dbml file will be added under App_Code folder. enter image description here

dbml

Expand Server Explorer and Right click on the Data Connections and select the option Add Connection.

enter image description here add-connection

Add Connection Pop up will be opened, give the SQL Server details and select the then click on OK button.

server-detail

Database will be added under Data Connections as shown below.

server-explorer

Drag the table in the left pane and if primary key & foreign key relations are there then it will automatically show as shown below. enter image description here

drag-tables

Drag the stored procedures to the right pane.

drag-store-procedures

Related C# code will be automatically generated and we can see by opening the file MyDB.designer.cs.

dbml-designer-cs

like image 29
aseman arabsorkhi Avatar answered Sep 22 '22 18:09

aseman arabsorkhi


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:

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

like image 27
HitLikeAHammer Avatar answered Sep 22 '22 18:09

HitLikeAHammer