Open your visual studio 2010 and create a new web project and then click on add new->data->Ado.net Entity Model option as shown below:
Then in the next screen you will have an option to create model from database or create an empty data model as shown below (We will select Generate from database option)
In the next screen you will have an option to specify connection string for the database from where you have to generate your data model. I have a Database named HillsDentalPOS so I will use that:
If you don’t have any database then you can click on new connection and add your database.
In the next screen we have to select Tables, Views and/or Stored procedures for which want our model to generate code.
And apoun clicking “Finish” an entity diagram is generated which seems like a UML diagram as shown above.
Above Diagram also contains all the associations auto generated like one-to-one one-to-many etc.
Now we will use this auto generated code to query our database and fetch data:
So as shown above we have instantiated an object our entity model and intellesense shows we have a lot of readymade methods available (More on it later). This also contains all the tables which were in our Database named “HillsdentalPOS” converted to corresponding classes in the code.
Now this query object contains our results/data in the form of an ienumerable object and hence we can put a foreach loop and fetch our results as:
Intellesense would show all the columns inside the tables in our db which have been converted to class and its attributes respectively. It’s so simple isn’t it?