How to Add Code First Model from Database in Web API
Follow the steps given below to add Entity Framework Code First Models from Database in your existing Web API or MVC project:
Step 1: Right click on "Models folder" in your Web API project and select "Add=>New Item.."
Step 2: In the type selection on left side, click on Installed=>Visual C#=>Data
Step 3: Click on "ADO.NET Entity Data Model" and give it a name as shown below and click on Add button:
Step 4: Select "Code First from Database" in Choose Model Contents popup window as shown below and click on "Next >" button:
Step 5: In the Choose your Data Connection window, click on New Connection... button, Select "Microsoft SQL Server" as your Data Source, click on Continue button, In the Connection Properties window, select your Data Source Server name and database, click on "Test Connection" button, to test the connection, click on OK button if the connection is succeeded:
Step 6: Select the "Yes, include the sensitive data in the connection string." radio button and click on "Next" button
Step 7: In the Choose Your Database Objects and Settings window, select the "Tables" checkbox, if you want to add only Tables. Keep "Pluralize or singularize generated object names" checkbox selected, this will Add Entity names in Singular but entity set names in Pluralize. Now click on "Finish" button.
This will add Code First Model classes to the Models folder of the project.
Note: If Entity Framework is not added to the project before adding Models through Code First approach, then no need to add Entity Framework manually, because while adding Code First Models, it will automatically add latest Version of Entity Framework to the project.