Every application needs data. Data needs to be fetched from a storage. Storage is a database. So the basic building block of an application is a full fleshed Database. A perfectly structured database is must for the smooth working of the application. So, without wasting time, let’s create a sample database, so as to let you know how to create one.
I have here, SQL Server R2, and I am going to show in that, how to create a database.
Right click on the database and go to New Database.
In the dialog box opened, give the name to the database, in the case SampleDatabase.
Click on Ok button, that’s it. Your database is created. Isn’t that simple? Now the database is ready to create table in it.
I’ll try to keep the schema (table’s structure) as simple as possible. We will be having two tables, “Category” and “FoodItem”. Now as you can see, a category can have many food items, like Fast food category can have, noodles (my favorite, which I am going to eat after completing this post,) Pizza, burger etc., likewise vegetable can have, Potatoes, Onion, cabbage etc..
So, our two tables will be having one to many relationship, that is, one category can have many food items.
Let’s first create these two table, without worrying about relationships as of now. In order to create table, expand out “SampleDatabase” go to Tables and Right click to choose New Table.
A raw table structure will open. Their add two columns, ID and CategoryName. From the context menu in column ID, set it as the primary key.
Save the Table and name it as Category. One more thing before is to set ID column as auto increment. To do this, expand the property below the table and set Is Identity property to Yes. Then save the table again.
Create another Table “FoodItem” with columns “ID”, “FoodItem”, “CategoryID” following the same above procedure, and if you have done it all correctly, it should look like this:
The tables are now ready, it’s time to make relationships between the tables. The column CategoryID in FoodItem table is basically the Foreign key of the table, and it should link with the Primary key of the category table. To do this, from the context menu for the CategoryID column, choose relationships. Click on Add in the dialog that opens and choose Table and column specification.
Click on the ellipses, that will open another dialog, map the two columns, CategoryID (the foreign key from food items) and CategoryID (the primary key of the Category table) as:
Click on OK and you are done. Our full fleshed database is ready. Now comes the main thing, the thing for which this post is all about, the Entity data model. Entity data model is basically a tier between database and application (to read more about entity data model go here.) So an application is first need to be created. Create an empty silverlight application, name it as SampleDatabaseApplication.
Data Model for the application is created in the server side project. So add an entity data model to the Web project by right clicking on the web project choosing New Item –> ADO.Net Entity Data model. Name the model as SampleDatabaseModel.
An entity data model wizard will open a soon as you click ok.
Click next as we want to generate the model from the database that we’ve created.
Choose New Connection as no connection is available as of now.
In the connection property windows enter the server name and select the database that we just have created. Test the connection, if it succeeds, click on OK button. Click on Next, thereafter.
Select the table that you want in your model. For now I need both, so I’ve selected both in the list. Click finish and done!
Congratulations, your Entity data model is created and ready to be used.
0 comments