Friday 9 May 2014

Database First development using Entity Framework



In today’s blog post we'll look at the new Entity Framework 5 and Visual Studio 2012. I'll discuss how you can approach it's usage from a Database First development using Entity Framework. Database first allows you to develop a model from an existing database. The model is stored in an EDMX file (.edmx extension) which can be viewed and edited in the Entity Framework Designer. The classes are automatically generated from the EDMX file to interact with your application.

You will need to have Visual Studio 2012, SQL Server, and SQL Server management Studio installed to complete the following sample application.
Steps to create tables and relationships in SQL server
1.    Open Microsoft SQL server management studio
2.    Right click Database folder and click New Database
3.     Type a name for the database (In this example I have used the database name as StudentsShaDatabase) and click ok


4.    Click the + of the database created; now you will see a folder called Table. Right click table and click New Table. 


5.    In this sample,  I have created 3 table Course Table, Enrolment Table and Student Table




6.     In order to make a column a  primary key right click the field and click “Set primary Key



7.    In this example, I have used automatic generation of primary key values, by choosing yes for Is Identity in the Identity Specification; Identity Seed is set as ‘1; to auto increment the number each time a row is inserted.

8.    After creating the tables, double click Database Diagram folder of the database created.

You will now see a prompt asking you if you need a database diagram to be created. Click yes. If you don’t receive any alerts simply right click the Database Diagram folder and choose New Database Diagram.

9.    Add all the 3 tables created. Please don’t get confused with the following table and field names it is only an example.



10. Now it is the time to set the foreign key to establish the relationship between the tables.
11. Click the (yellow) primary key of the one table and drag it to the foreign key column of another table. Now the relationship of 1: N is established between them.


12. If the Foreign key Coolum name is not selected automatically, click the drop down button select the correct column name and click ok.


13. After creating all the relationships, our database will be ready to be used in our application. 

Developing the MVC4 project using Entity Framework in Visual Studio 
1.    Open Visual Studio 2012
2.    Click File New Project and select ASP.NET MVC4 application.


3.    In this scenario the project name I have given is “MVC4StudentsApplication”
4.    Copy the database. mdf created previously, and paste it inside a newly created folder within the project . I have created a folder called “source” inside my main project folder (MVC4StudentsApplication), and a “sub folder” named assets and pasted my mdf file inside it.

5.    Right click App_Data and click Add an Existing item. Browse and select the .mdf file inside the assets folder and select Add.



6.    The next step is to add ADO.NET data model.
7.    Right click the project folder and click Add ADO.NET Entity Data Model, give a name “StudentsDataModel” and click ok.



8.    Select EF Designer from Database and click Next


9.    Configure the connection settings and click next.


10. Choose Entity Framework 5.0 and click next.


11. Tick All the tables , and click Finish 
12. The controllers and corresponding views for each table can be hereafter generated automatically  by right clicking controller and Add -- Controller



13. Give a name for Controller; choose the table name (Eg. Course, Student )  in the “Model Class” and the connection string name in “Data Context class” section and click Add. Instantly the corresponding view is generated automatically. For the Index and a few other functions “eg Detail” you may need to replace the correct primary key in the generated code, to enable the correct execution of the program.

14. In the shared Layout file you need to insert the necessary lines of code which will display a menu to call the controllers.

15. You can download the source code of the program from https://github.com/shaliniac/ShaliniNETSamples/tree/master/samples the project is called MVC4StudentsApplication.  

THE END



No comments:

Post a Comment