Over a million developers have joined DZone.

Adding the LINQ to SQL ORM to Your .NET Project

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Coucbase.

I am working on a new personal project (I mentioned it in my videocast), I am building it in MVC, since it is a small project I wanted an easy to use and implement ORM. I chose LINQ to SQL after hearing several good things. It has been a real breeze to set up and use. So, without further ado, I have put together a tutorial on how to set up LINQ-TO-SQL.

 



First add a new Linq to SQL class to your project. Since I'm using MVC, I'm putting my Linq to SQL classes in my Model folder. However, you can put them wherever you want. When I add the classes I get the Object Relational designer telling me to drag over items from my server explorer to map them.


Now you need to add your SQL server to your project's Server Explorer. Right click on data connections and go to add a new connection.  Put in the ODBC info for your server, and make sure to test your connection before closing. Now, if you expand your tables all you have to do is drag them over to the OR Designer.

You can very easily add relationships to your objects. I'm not sure if that would automatically happen if they are related in the DB. Someone will need to try and let me know. In order to add a relationship you right click on one of your tables, click "Add" and choose "Relationship." You can then specify the parent and child tables as well as what columns hold the relationship.


Next I'm going to build an interface to list my methods that I'm going to build to deal with my objects quickly. As you can see, mine are specific to my application, but I think they will be helpful to you anyway.



To create these methods, first I'm going to create an instance of the Data Context that was created with my .dbml. Then I'm going to use LINQ to query my objects for me and return them. I'm just making a basic Save method, and some small queries that I think I will need.


There you have it. That's how easy it is. I KNOW! You can download my source here. Now we have my model set up we can move to the rest of our MVC for web forms developers series. Stay tuned!!

 

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:

Published at DZone with permission of Sara Chipps, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}