How to Create Model Classes with Entity Framework(C#)?

Entity Framework application uses a class derived from DbContext. This derived class will call one of the constructors on the base DbContext class to control:
  • How the context will connect to a database—i.e. how a connection string is found/used
  • Whether the context will use calculate a model using Code First or Database First
Step 1: Create the Database and the Tables you want to use

Step 2: Right-click the Models folder in the Solution Explorer window and select the menu option Add, New Item.

Step 3: In the Add New Item dialog, select the Data category and select the ADO.NET Entity Data Model template, give the Entity Data Model the name Ex: Demo.edmx, and click the Add button. Clicking the Add button launches the Data Model Wizard.


Step 4: In the Choose Model Contents step, choose the EF Designer from a database option and click the Next button

Step 5: In the Choose Your Data Connection step, select the New Connection, enter the server name, username and password and click the OK button


Step 6: In the Choose Your Database Objects step, select the tables and click the Finish button.
After you complete these steps, the ADO.NET Entity Data Model Designer (Entity Designer) opens

Modifying the ADO.NET Entity Data Model

After you create an Entity Data Model, you can open the Entity Designer at any time by double-clicking the Demo.edmx file contained in the Models folder within the Solution Explorer window.

Note : Remember to save your Entity Data Model after making a modification by clicking the Save button 


Comments