the Entity Framework offers 3 distinct methodologies for operating, each of which has its own advantages and disadvantages. The three distinct methodologies are as follows:
Code First Approach: This method is necessary when an application lacks an existing database. The Code-First Approach commences with the development of our domain entities (domain classes) and context class. Subsequently, the entity framework generates the database based on the domain classes and context class.
Model-First Approach: We employ a visual EDMX designer to develop our models. Thus, in this method, we directly construct entities, the context class, and the database script from your visual model, after creating the relationships, inheritance hierarchies, and entities on the visual designer
Database First Approach: If the database schema is already in existence, the Database First Approach must be implemented. The Entity Data Model generator is employed to generate the context and entities for the existent database in this method. This method is most appropriate for applications that utilise an existing database.
A Few Critical Points of the EF Code First Approach:
- The Entity Framework Code First Approach is designed to construct a database and associated tables from scratch, assuming that the database does not already exist.
- The Entity Framework Code First Approach can also be implemented with an empty database. In this scenario, Code First will incorporate new tables into the existing database that is currently vacant.
- It enables us to specify our domain model by utilising C# or VB.NET classes.
- It is feasible to implement additional configuration through the utilisation of attributes on our classes and properties or through the utilisation of a fluent API.
Database Initialization in Entity Framework Code First Approach:
the Entity Framework Code-First Approach creates a database automatically
- Calling DbContext Class Constructor with No Parameter:
- Call the DbContext class constructor which does not take any parameter from our user-defined context class, then Entity Framework API will create a local database in your local SQLEXPRESS server with the name that matches our {Namespace}.{Context class name}
- Calling DbContext Class Constructor by Passing Database Name
- If we pass the database name as a parameter to the DbContext Class Constructor from our context class, then Entity Framework Code First Approach will create a database automatically again in the local SQLEXPRESS database server. But this time the name will be the one that is passed as a parameter to the DbContext Class constructor.
- Calling DbContext Class Constructor by Passing Connection String Name
- You can also define the connection string in the app.config or web.config file and specify the connection string name starting with “name=” in the base constructor of the context class. So, this is an easy way to tell the DbContext class to use a database server instead of local SQL Express or LocalDb.
OR
If the connection string name is different from the name of your context class, then you need to tell the DbContext class to use the connection string in Entity Framework Code First Approach by passing the connection string name as a parameter to the DbContext class constructor. The connection string name must start with “name=” otherwise, it will consider as a database name. So, first, modify the context class as shown in the below code.
- You can also define the connection string in the app.config or web.config file and specify the connection string name starting with “name=” in the base constructor of the context class. So, this is an easy way to tell the DbContext class to use a database server instead of local SQL Express or LocalDb.
When Database is Created in CFA In the code-first approach, data is not created every time you run your application. Model Definition: You start by defining your data model as classes in your code (e.g., using Entity Framework). These classes represent the tables and columns in your database. Database Generation (Initial Creation): When your application runs for the first time (or when the database doesn't exist), Entity Framework (or your chosen ORM) will use your code-defined model to automatically generate the database schema.
This involves creating the necessary tables, columns, relationships, and constraints. Subsequent Runs: On subsequent runs of your application, the ORM typically checks if the database schema is compatible with your current code model. No Data Re-creation by Default: By default, the code-first approach does not drop and recreate the data in your database every time you run the application. Your existing data persists across application runs.'