dotConnect for Snowflake Documentation
On this page
    Entity Framework Core Support
    On this page

    dotConnect for Snowflake supports Entity Framework Core. This topic describes the supported versions and how to configure the provider. The following topics describe the other aspects of using Entity Framework Core with Snowflake:

    What is Entity Framework Core?

    Entity Framework Core is a lightweight, extensible, and cross-platform object-relational mapper (ORM) for .NET. It enables developers to work with data using .NET objects and LINQ queries instead of writing most of the data access code manually. Entity Framework Core applications provide the following benefits:

    Supported Entity Framework Core Versions

    Entity Framework Core support is provided by the Devart.Data.Snowflake.EFCore NuGet package. The same package is released in several builds, one for each supported Entity Framework Core version. The build is identified by the last part of the package version: it matches the major version of Entity Framework Core. The package version without the fourth part is intended for Entity Framework Core 3.1.

    Entity Framework Core

    Package version

    Supported platforms

    3.1X.Y.Z.NET Framework 4.6.1 and higher, .NET Core 3.1 and higher
    8X.Y.Z.8.NET 8 and higher
    9X.Y.Z.9.NET 8 and higher
    10X.Y.Z.10.NET 10 and higher
    11X.Y.Z.11.NET 11 and higher

    X.Y.Z is the version of dotConnect for Snowflake. For example, for Entity Framework Core 9 and dotConnect for Snowflake 1.2.3, use the Devart.Data.Snowflake.EFCore package version 1.2.3.9.

    By default, NuGet installs the latest package version, which is the build for the latest supported Entity Framework Core version. If your application uses an earlier Entity Framework Core version, specify the package version explicitly when adding the package. To install the package for Entity Framework Core 9, run one of the following commands, replacing X.Y.Z.9 with the required package version:

    Install-Package Devart.Data.Snowflake.EFCore -Version X.Y.Z.9
    dotnet add package Devart.Data.Snowflake.EFCore --version X.Y.Z.9
    

    Entity Framework Core Support Key Features

    Using the Entity Framework Core Provider

    Add the Devart.Data.Snowflake.EFCore NuGet package of the version that corresponds to your Entity Framework Core version to your project (see Supported Entity Framework Core Versions).

    To register the Entity Framework Core provider and set a connection string, override the OnConfiguring method of your DbContext class descendant and call the UseSnowflake method. It accepts either a connection string or an existing connection object.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      optionsBuilder.UseSnowflake(@"Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********");
    }
    

    The Snowflake schema specified in the Schema connection string parameter, together with the database from the Database parameter, acts as the database of the model. For the authentication methods you can use in the connection string, see Authentication.

    The UseSnowflake method has two overloads: UseSnowflake(string connectionString) and UseSnowflake(DbConnection connection). If you pass a connection object, it must be a SnowflakeConnection; a connection of another type causes InvalidOperationException. The method returns a SnowflakeDbContextOptionsBuilder object (the Devart.Data.Snowflake.Entity namespace). To set relational options, such as the command timeout or the migrations history table, call the corresponding methods of this object in a chain:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      optionsBuilder.UseSnowflake(connectionString)
        .CommandTimeout(120)
        .MigrationsHistoryTable("MigrationsHistory", "MIGRATIONS");
    }
    

    Dependency Injection

    In ASP.NET Core and other applications that use dependency injection, register your DbContext class using the AddDbContext or AddDbContextPool method. Retrieve the connection string from the application configuration instead of storing it in the source code.

    Define a constructor that accepts a DbContextOptions<TContext> object:

    public class ShopContext : DbContext {
      public ShopContext(DbContextOptions<ShopContext> options)
          : base(options) { }
    
      public DbSet<Product> Products => Set<Product>();
    }
    
    // Program.cs
    builder.Services.AddDbContext<ShopContext>(options =>
      options.UseSnowflake(builder.Configuration.GetConnectionString("Snowflake")));
    

    Store the connection string under the ConnectionStrings:Snowflake configuration key. Use a secure configuration source, such as user secrets during development or a secrets store in production.

    For recommendations on protecting authentication credentials, see Security Best Practices.

    The MaxBatchSize option has no effect: the provider executes every INSERT, UPDATE, and DELETE statement as a separate request. For more information, see Saving Changes and Transactions and Bulk Operations and Performance.

    Date and Time Values

    By default, the UTC Dates connection string parameter is false. In this mode, a DateTime parameter value is converted from the local time of the client machine to UTC before it is sent, and TIMESTAMP_NTZ values are read as local time. As a result, comparisons of TIMESTAMP_NTZ columns with DateTime parameters, date parts, and date arithmetic calculated on the server can differ from the values you work with in the application by the time zone offset of the client machine. For Entity Framework Core applications, we recommend adding UTC Dates=true to the connection string: in this mode, DateTime values are sent and read as they are, and queries over TIMESTAMP_NTZ columns return the same results as the corresponding .NET code.

    Database-First

    Database-First is supported via the Scaffold-DbContext command in Package Manager Console or the dotnet ef dbcontext scaffold command in .NET CLI. The model is read from INFORMATION_SCHEMA and from the primary keys, unique keys, foreign keys, and sequences of the database.

    By default, the tables and views of all schemas of the current database (the Database connection string parameter), except INFORMATION_SCHEMA, are scaffolded, not only the tables of the schema specified in the connection string. To limit the model, use the -Schemas and -Tables parameters (--schema and --table in .NET CLI). Views are scaffolded as keyless entity types mapped to views; with Entity Framework Core 3.1, they are scaffolded as tables. Comments of tables and columns are scaffolded too.

    Scaffold-DbContext "Domain=myorg-myaccount.snowflakecomputing.com;User=your_user;Password=********;Database=your_database;Schema=your_schema;Warehouse=your_warehouse;License Key=**********" Devart.Data.Snowflake.EFCore -OutputDir Models
    

    If the default value of a column is a call of a sequence (sequence.NEXTVAL), the scaffolded model keeps it, and the values are generated in the same way as for keys in a Code-First model: the provider takes the next value of the sequence before the INSERT. This applies to key columns and to other columns with such a default value. Columns created with IDENTITY or AUTOINCREMENT are scaffolded as columns whose value is not generated, and a warning is shown: the provider can't read back a value generated by Snowflake (see Generating Key Values).

    Code-First

    Code-First is supported with the EnsureCreated method and migrations. Don't use EnsureCreated and migrations to manage the same database schema: a schema created by EnsureCreated has no migrations history and isn't designed to be updated with migrations. EnsureCreated creates the database and the schema specified in the connection string, if they don't exist, and the tables and sequences of the model. Creating a database requires the CREATE DATABASE privilege on the Snowflake account.

    By default, EnsureDeleted drops the tables and sequences of the model and the migrations history table. It never drops the database, because the database holds all schemas, not only the schema of the model. What EnsureDeleted drops is determined by the SnowflakeEntityProviderConfig.Instance.DatabaseScript.Schema.DeleteDatabaseBehaviour property (the Devart.Data.Snowflake.Entity.Configuration namespace). It accepts the following values of the DeleteDatabaseBehaviour enumeration:

    using Devart.Data.Snowflake.Entity.Configuration;
    ...
    SnowflakeEntityProviderConfig.Instance.DatabaseScript.Schema.DeleteDatabaseBehaviour =
      DeleteDatabaseBehaviour.AllSchemaObjects;
    context.Database.EnsureDeleted();
    

    The setting applies to all DbContext instances of the application.

    Saving Changes and Transactions

    Deploying an Entity Framework Core Project

    When deploying Entity Framework Core projects, make sure that the Devart.Data.dll, Devart.Data.SqlShim.dll, Devart.Data.Snowflake.dll, and Devart.Data.Snowflake.EFCore.dll assemblies are available for your application. If you reference the provider via NuGet packages, these assemblies are copied to the output folder automatically. You don't need to register the provider in the machine.config or app.config file.

    Limitations