# Connect using Entity Framework Core

> **📝 Note**: This feature is only supported in SingleStore clusters running SingleStore version 7.8+.

Entity Framework (EF) Core serves as an object-relational mapper (O/RM) that provides an automated mechanism to the developers to access and store data in a database using .NET objects. The [EntityFrameworkCore.SingleStore](https://www.nuget.org/packages/EntityFrameworkCore.SingleStore/) provider allows you to use EF Core with your SingleStore databases. This provider is built on top of the [SingleStore Connector for .NET and .NET Core](https://www.nuget.org/packages/SingleStoreConnector).

## Connect with SingleStore using EF Core

To connect with SingleStore:

1. **Install the provider**: Run the following command:
   ```shell
   dotnet add package EntityFrameworkCore.SingleStore
   ```
   The following reference is added to the **.csproj** file in your project:
   ```xml
   <PackageReference Include="EntityFrameworkCore.SingleStore" Version="8.0.0" />
   ```
   If the package reference is not present in the **.csproj** file in your project, add it.

2. **Configure the service**: Add `EntityFrameworkCore.SingleStore` to the services configuration in the **Startup.cs** file of your .NET Core project. Here's a sample:
   ```C#
   public class Startup
   {
       public void ConfigureServices(IServiceCollection services)
       {
           // Replace with your connection string
           var connectionString = "server=<endpoint>;user=<username>;password=<password>;database=<database>";
       
           // Replace 'DbContext' with the name of your own DbContext derived class.
           services.AddDbContext<DbContext>(
               dbContextOptions => dbContextOptions
                   .UseSingleStore(connectionString));
       }
   }

   ```

For more information, refer to [Configuration Options](https://github.com/memsql/SingleStore.EntityFrameworkCore/wiki/Configuration-Options).

## Scaffolding (Reverse Engineering)

You can scaffold (reverse engineer) entity type classes and a `DbContext` class based on a database using the `dotnet ef dbcontext scaffold` command. To run scaffolding commands, you must have [.NET Core CLI tools](https://learn.microsoft.com/en-us/ef/core/cli/dotnet) and the `EntityFrameworkCore.SingleStore` package. Use the following command to install `dotnet ef`:

```shell
dotnet tool install --global dotnet-ef
```

To verify the installation, run the following command:

```shell
dotnet ef
```

To generate code for a `DbContext` for a SingleStore database using EF Core, run the following command:

```shell
dotnet ef dbcontext scaffold <connection-string> "EntityFrameworkCore.SingleStore"
```

Here's an example:

```shell
dotnet ef dbcontext scaffold "Server=svchost;Port=3306;Uid=root;Pwd=passw0rd;database=acme" "EntityFrameworkCore.SingleStore"
```

## Unsupported Features/Limitations

* SingleStore does not support **foreign keys** and **referential integrity**. Therefore, while performing [EF Core migrations](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/), foreign keys are not created between the entities.
* SingleStore does not support updating the `@@session.time_zone` variable at runtime. This variable exists only for MySQL compatibility and is always set to `SYSTEM`.

  To modify the time zone for a session, specify a session time zone offset in the provider. For example:
  ```c#
  optionsBuilder.UseSingleStore(cs, o => o.SessionTimeZone("-08:00"));
  ```
  By default, the offset is set to `+00:00` (UTC).
* `EF.Functions.Collate` is not supported, because you cannot change a column's collation in a SingleStore database.
* Additional unsupported features:

  * Savepoints
  * Unique indexes/constraints
  * Check constraints
  * Spatial data types
  * Full-text indexes
  * JSON
* `AUTO_INCREMENT` can only be used on a `BIGINT` column.
* By default, max key length is set to `3072` (same as MySQL). Contact [SingleStore Support](https://support.singlestore.com) to increase the length. SingleStore does not impose any limitations on the key length.

Refer to [Unsupported MySQL Features](https://docs.singlestore.com/db/v9.1/connect-to-singlestore/connect-with-mysql/connect-with-mysql-client/unsupported-mysql-features.md) for more information.

## Example

The following example performs CRUD operations in SingleStore using EF Core:

1. Clone the following GitHub repository: <https://github.com/singlestore-labs/start-with-singlestore-csharp-orm.git>.

2. Run the following command in the command-line:
   ```shell
   dotnet restore
   ```

3. Update the placeholders in the connection string in the **appsettings.Development.json** file with the connection configuration for your SingleStore database:
   ```json
   "DefaultConnection": "Server=<hostname-or-ip-address>;Port=3306;Uid=<user>;Pwd=<password>;database=<database-name>"
   ```
   Here's a sample:
   ```json
   {
     "Logging": {
       "LogLevel": {
         "Default": "Debug",
         "System": "Information",
         "Microsoft": "Information"
       }
     },
     "ConnectionStrings": {
       "DefaultConnection": "Server=svchost;Port=3306;Uid=root;Pwd=passw0rd;database=acme"
     }
   }
   ```

4. Run the commands in the **init.sql** file in your SingleStore database:
   ```shell
   singlestore -h svchost -P 3306 -u admin -p passw0rd -D acme < init.sql
   ```
   You can also run the commands in the **init.sql** file directly in your SingleStore database.

5. Run the following command to build the project and all of its dependencies:
   ```shell
   dotnet build
   ```

6. The **Runner.cs** file in this repository defines basic CRUD operations using EF Core. Run the following command to perform these operations:
   ```shell
   dotnet run
   ```
   Here are snippets from the output that highlight each operation:

   **Create Operation:**
   ```shell
   Executed DbCommand (515ms) [Parameters=[@p0='?' (Size = 4000), @p1='?' (DbType = DateTime)], CommandType='Text', CommandTimeout='30']
         INSERT INTO `messages` (`Content`, `CreateDate`)
         VALUES (@p0, @p1);
         SELECT `Id`
         FROM `messages`
         WHERE ROW_COUNT() = 1 AND `Id` = LAST_INSERT_ID();
   Inserted row id 3377699720527873
   ```
   **Read Operation:**
   ```shell
   Executed DbCommand (390ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
         SELECT `m`.`Id`, `m`.`Content`, `m`.`CreateDate`
         FROM `messages` AS `m`
         ORDER BY `m`.`Id`
   Read all rows:
   3377699720527873, Updated row
   ```
   **Update Operation:**
   ```shell
   Executed DbCommand (519ms) [Parameters=[@p1='?' (DbType = Int64), @p0='?' (Size = 4000)], CommandType='Text', CommandTimeout='30']
         UPDATE `messages` SET `Content` = @p0
         WHERE `Id` = @p1;
         SELECT ROW_COUNT();
   Updated row id 3377699720527873
   ```
   **Delete Operation:**
   ```shell
   Executed DbCommand (451ms) [Parameters=[@p0='?' (DbType = Int64)], CommandType='Text', CommandTimeout='30']
         DELETE FROM `messages`
         WHERE `Id` = @p0;
         SELECT ROW_COUNT();
   ```

## References

* [Overview of Entity Framework Core](https://learn.microsoft.com/en-us/ef/core/)
* [Entity Framework Core Tools Reference](https://learn.microsoft.com/en-us/ef/core/cli/dotnet)

***

Modified at: February 16, 2026

Source: [/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-net-and-net-core/connect-using-entity-framework-core/](https://docs.singlestore.com/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-net-and-net-core/connect-using-entity-framework-core/)

(An index of the documentation is available at /llms.txt)
