Creating data access layer using Entity Framework using AdventureWorks database

 

In the previous post you have create the project to work on the AdventureWorks web API application  . Next we are going to add the data access layer using the Entity Framework.

Entity Framework is a tool which maps the database objects to the model objects. It provides the medium to execute the CRUD command to the database without writing any SQL statement. for more information on Entity Framwork you can visit this link

Next we are going to add ado Entity model for our project Right click on the Model folder in the solution explorer , go to add and then click on the ADO.NET Entity Data Model.

image

It will ask to the Name

image

change Item Name to “AdventureWorksDB” and click on OK

image

It will display Entity Data Model wizard

image

Select “Generate from database” and click on Next.

Now it will ask you to choose data connection.

image

Next click on the new connection , it will ask you for the connection properties.

image

Enter server name , authentication type(Windows or SQL) and then select the database , after it click on the Test connection button  to make sure what you have entered is connect.

image

if it succeeded then we are ready to go. click on the ok button on both window.You will come back to choose connection window.

image

For this time , we are selecting Yes from the options button, after it click on next , it will ask you to select database tables , views stored procedure, select all and then update the model name to “AdventureWorksModel”

image

After that click on finish button. it will open the model design  file.

image

  Now the interesting thing , you got the blank diagram file which is not expected.

image

This the because of the Production.Document table

image

Lets take a look this table.So the problem is the Entity framwork not understand the hierarchyId data type.

image

For now the easiest workaround is to exclude this table (we will fix it in forthcoming posts) and select others.Right click on the diagram file and select “Update Model from Database”

image  

it will display the Update Wizard , select all but not Document table

image

we are done, just save the file and rebuild the solution again using the build menu.

image

Thanks for reading , hope you find this post useful . 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s