Model Context Protocol (MCP) finally gives AI models a way to access the business data needed to make them really useful at work. CData MCP Servers have the depth and performance to make sure AI has access to all of the answers.
Try them now for free →Entity Framework: Code-First vs Model-First
Entity Framework (EF) is an object-relational mapping (ORM) framework for .NET applications, allowing developers to work with relational database data using .NET objects. When developers use EF, they must first choose between the Code-First and Model-First approaches for database modeling.
In this article, we’ll review both approaches, compare their major advantages, and provide examples for using both methods with the CData ADO.NET Provider for CSV.
NOTE: While this article refers to using CSV as a data source, the principles can be applied to any of the 270+ data sources CData supports.
Code-First
The Code-First approach allows developers to define the data model using C# or VB.NET classes, and EF generates the database schema based on the created classes. This method gives developers full control over the exact structure of the data model in their .NET application. This method is ideal for situations where the database schema is expected to evolve or shift with the application.
Advantages
- Developer controlled: The developers have complete control over writing the code for the data model, allowing any changes to the database schema to be handled with code.
- Database flexibility: The Code-First approach allows a developer to manually customize the data model classes, allowing for greater flexibility.
Model-First
The Model-First approach allows developers to create a database model using the Entity Data Model wizard; then, EF generates the database schema based on the model. The Entity Data Model wizard creates an EDMX file (.edmx) that stores the data model. This method is ideal when a visual representation of the database schema is needed before writing any code.
Advantages
- Ease of use: The Model-First approach is ideal for beginners or developers who want a quick start to their data model because EF handles a large part of the coding work up front for the database schema.
- Saving Time: The Entity Data Model wizard can create data models based on large and complex databases in a very short time when compared to hand-coding the model.
Examples
Code-First
Below are the steps to access CSV data using the code-first approach to Entity Framework with the CData ADO.NET Provider for CSV:
-
Create the application
Open Visual Studio and create a new “Console App (.NET Framework)” project.
-
Install Entity Framework and add references to the required assemblies
-
Run the following command to download and install Entity Framework automatically:
Install-Package EntityFramework
- If you are using EF 6, add a reference to System.Data.CData.CSV.Entities.EF6.dll, located in the lib subfolder of the CData ADO.Net Provider installation directory, by default: “C:\Program Files\CData\CData ADO.NET Provider for CSV 2024\lib”.
-
Run the following command to download and install Entity Framework automatically:
-
Register the provider
Modify the App.config file for your project to add a reference to the CSV Entity Framework 6 assembly.
… … -
Create the context class
To create the CSVContext class, you will need to add a new .cs file to the project. The CSVContext class is your base database context that extends DbContext and exposes the DbSet properties that represent the tables in the data source. The following code example overrides the OnModelCreating method, removing PluralizingTableNameConvention from the ModelBuilder Conventions and removing requests to the MigrationHistory table:
using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.Entity.ModelConfiguration.Conventions; class CSVContext : DbContext { public CSVContext() { } public DbSet<Customer>Customer { set; get; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { // To remove the requests to the Migration History table Database.SetInitializer<CSVContext>(null); // To remove the plural names modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }
-
Ceate the table models
Create another .cs file and name it after the CSV entity you are retrieving, for example, Customer. In this file, define both the Entity and the Entity Configuration, which will resemble the example below:
using System.Data.Entity.ModelConfiguration; using System.ComponentModel.DataAnnotations.Schema; //EF 6 and later //using System.ComponentModel.DataAnnotations //For versions of EF before 6 [System.ComponentModel.DataAnnotations.Schema.Table("Customer")] public class Customer { [System.ComponentModel.DataAnnotations.Key] public System.String City { get; set; } public System.String TotalDue { get; set; } }
-
Query the data in your code
After the context and entity are complete, you can now query the data in a different class. For example:
CSVContext context = new CSVContext(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Customer select line;
Model-First
Below are the steps to access CSV data using the Entity Framework model-first approach (C#) with the CData ADO.NET Provider for CSV. This example is from Visual Studio 2022 and uses Entity Framework 6:
-
Create the application
Open Visual Studio and create a new “Console App (.NET Framework)” project.
- Install Entity Framework and add references to the required assemblies
-
Register the provider
Modify the App.config file for your project to add a reference to the CSV Entity Framework 6 assembly.
… … -
Add a Data Connection in Server Explorer
Before creating an Entity Data Model, you will need to create a data connection. To do this:
- Navigate to Server Explorer -> right-click "Data Connections" -> click “Add Connection...”.
- Click the “Change” button beside the Data Source box.
- Select “CData CSV Data Source”, then click “OK”.
-
Configure your connection settings, then click “OK”.
-
Create a data model with the Entity Data Model Wizard
- In the Visual Studio Solution Explorer, right-click your project and click “Add” > “New Item”.
-
In the resulting Add New Item dialog, click ADO.NET Entity Data Model and enter an appropriate title. For example: "CSVEntityDataModel.edmx".
-
This opens the Entity Data Model Wizard. On the first page, select “EF Designer from database” and click “Next >”.
-
On the next page, select the data connection that was previously configured. Ensure you check the box to save the entity connection settings in App.Config, and enter a name for the context class of the data connection; for example, "CSVEntities", then click “Next >”.
-
After Visual Studio retrieves the necessary information from the live data source, the wizard lists database objects you can include in your project. Select the objects you want to add, enter a Model Namespace, and then press “Finish”.
-
Query the data in your code
After the Entity Data Model Wizard creates the data model, you can now query the data in a different class. For example:
CSVEntities context = new CSVEntities(); context.Configuration.UseDatabaseNullSemantics = true; var query = from line in context.Customer select line;
Free Community License for data developers
CData ADO.NET Providers further enhance the capabilities of Entity Framework by offering consistent connectivity to more than 270 data sources beyond traditional databases, including SaaS, NoSQL, and big data systems.
With the CData ADO.NET Community License, you get free-forever libraries to access your data in personal .NET projects, all through familiar SQL. Request a license and start building data apps today!