ADO.NET Linq to Entity


6/6/2009 6:23:48 PM

I have recently been doing quite a bit of work with ADO.NET Linq to Entities. So, I thought that I would share what I have learned so far. Some of the main points I will be touching on are setting up your connection to your database and your data model.

I use Sql Server 2008 Express Edition that Microsoft releases as a free database tool. Obviously, it does not have the full power of SQL Server full release, but it does the trick for simple CRUD operations. Also, it is worth mentioning that I program using ASP.NET C# Visual Studios 2008 with service pack 1.

First thing when using your data model is making that connection to your database. In order to do this you can simply:

  • Right click on your project
  • Select add new item
  • Select ADO.NET Entity Data Model under Visual C# Categories
  • Name it something sensible like DataModel.edmx
  • Press Add

A wizard window should pop up called Entity Data Model Wizard. This wizard will guide us through the process of making that connection to our database. Next we will:

  • Click on Generate from Database and click next.
  • Next we will choose our data connection. Click on New Connection.
  • This should open up a Connection Properties window.
  • Select your server name.
  • Select the Database on the server and test your connection to make sure your database is there.
  • This should take us back to the wizard.
  • Click Next.
  • In the Choose Your Database Objects window select what tables, stored procedures or Views you would like to deal with and click on Finish.

And you should have successfully wired up your Database to talk to your program. Now you can use your Entities connection string to access all of your tables.

My next tutorial will be actually using our Data Model to talk to our tables and extract some data, manipulate it and send it back to the Database.