Following steps guide us to the process to add data model using Entity Framework through VS 2012:
1. Open up ASP.NET MVC 4 application
2. Now, create ADO.NET Entity Data Model using any one of the below procedures:
Right click on the Model folder and select Add => ADO.DB Entity Data Model, as shown in Figure 1 then type name of the data model and specify Name in Item dialog.
For example, enter DataModel shown in Figure 2 and click “OK” button for starting the Entity Data Model Wizard that will guide us through steps to create the Entity Framework based data model.
Alternatively, we can select Add => New Item. In the Add New Item dialog (see Figure 3) then click “Data” from the left pane and click “ADO.NET Entity Data Model” from middle pane. Enter a name such as DataModel in Name textbox. and click the “Add” button to launch Entity Data Model Wizard.
1. Open up ASP.NET MVC 4 application
2. Now, create ADO.NET Entity Data Model using any one of the below procedures:
Right click on the Model folder and select Add => ADO.DB Entity Data Model, as shown in Figure 1 then type name of the data model and specify Name in Item dialog.
Figure 1. ADO.NET Entity Data Model of context menu
Figure 2. Naming the ADO.NET Entity Data Model
Figure 3. Creating the ADO.NET Entity Data Model via the Add New Item dialog
3. From Choose Model Content page of the Entity Data Model Wizard, as shown in Figure 4, we decide which approach to be used for the model: database first is “Generate from
database” or code first “Empty model”. In our case, model will be generated on the basis of existing tables from the database. So, select “Generate from database” and then click “Next.”
Figure 4. choosing the database - first approach of generating the data model
4. Next step of the wizard is Choose Your Data Connection, shown in Figure 5. The connection includes name of the server (or IP) where database resides database name and the connection credentials. Steps of this wizard given below:
Figure 5. Generating and saving the Entity Framework connection string
a. The wizard asks which data connection our application should use to connect to destination database. Only one option available in the drop-down list that is DefaultConnection, so we don’t need to change it. Connection was created by ASP.NET MVC 4 application and it is used to connect the Local DB where our table resides.
Alternatively, if database is not already created, you may click the “New Connection”
button to create new connection string. It opens the Choose Data Source dialogbox,
from which we identify the type of DB we are connecting to (for example, SQL Server or
Oracle). Available data source depens on the Entity Framework provider installed. By default, we have MS SQL Server, MS SQL Server 4.0, and MS SQL Server DB File. For example, we are connecting to a Local DB then select MS SQL Server Database File and click “Continue.” Add Connection dialog will appear. we can either browse for the DB file or, if we know the exact location then type the location in the box. Select how we are connecting, with Windows or with SQL Server Authentication. We can leave the default
settings which is Windows Authentication. Now click the “Test Connection” button at the bottom of dialog to test connection. We should get a message “Test connection
succeeded.” then click “OK.” If we don’t get this message then something is wrong with
configuration and we should review entered values. Now click “OK” to finish the new connection. New connection will be available in drop-down list as shown in Figure 5.
b. “Entity connection string” section in the wizard steps preview the Entity Framework connection string which is generated based on database selection.
c. The check box at bottom of this steps give us choice of saving the Entity Framework connection string in Web.Config file. It is generally good idea to select this
option as it ensures that our connection string is defined at one place in application, which make it easier to maintain in future. So, check the box and enter name EntitiesConnection in field below it.
d. Click Next to proceed to next step of this wizard.
No comments:
Post a Comment