dotConnect for Snowflake Documentation
On this page
    Getting Started with Entity Framework Core
    On this page

    Learn how to use dotConnect for Snowflake with Entity Framework Core in a .NET console application. This tutorial walks you through installing the provider, defining an entity model, creating database objects, and adding and querying data in Snowflake.

    For the supported Entity Framework Core versions and the specifics of the provider, see Entity Framework Core Support.

    Prerequisites

    Create a Project and Install the Provider

    Create a console application and add the Devart.Data.Snowflake.EFCore NuGet package to the project. Specify the package version that matches your Entity Framework Core version and the target framework of the project. For example, for a .NET 10 project with Entity Framework Core 10, use the package version that ends with .10:

    dotnet new console -n SnowflakeEFCoreDemo
    cd SnowflakeEFCoreDemo
    dotnet add package Devart.Data.Snowflake.EFCore --version X.Y.Z.10
    

    Replace X.Y.Z with the version of dotConnect for Snowflake. In Visual Studio, you can also run the following command in Package Manager Console:

    Install-Package Devart.Data.Snowflake.EFCore -Version X.Y.Z.10
    

    If you don't specify the version, NuGet installs the latest package version, which is built for the latest supported Entity Framework Core version. If the target framework of your project doesn't support this build, the installation fails with the NU1202 error. For the list of package versions, see Supported Entity Framework Core Versions.

    Define the Entity Model and Context

    Add a file named ShopContext.cs to the project and define an entity class and a DbContext class. In the OnConfiguring method, call UseSnowflake to configure the provider and specify the connection string:

    using Microsoft.EntityFrameworkCore;
    
    public class Product {
      public int Id { get; set; }
      public string Name { get; set; } = string.Empty;
    
      [Precision(12, 2)]
      public decimal Price { get; set; }
    }
    
    public class ShopContext : DbContext {
      public DbSet<Product> Products => Set<Product>();
    
      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;UTC Dates=true;License Key=**********");
      }
    }
    

    The tables of the model are created in the schema specified in the Schema connection string parameter. The UTC Dates parameter is recommended for Entity Framework Core applications. For more information, see Using the Entity Framework Core Provider. For other authentication methods, see Authentication.

    Don't store credentials in the source code of a real application. For recommendations, see Security Best Practices. To retrieve the connection string from the application configuration, register the context using dependency injection. For more information, see Dependency Injection.

    Create Database Objects

    For a simple application or prototype, use the EnsureCreated method to initialize the database schema. The method creates the schema specified in the connection string if it doesn't exist, and the database objects required by the model: the "Products" table and the sequence used to generate key values. If the tables already exist, the method does nothing.

    If you plan to manage database schema changes over time, use migrations instead of EnsureCreated. For more information, see Migrations.

    Warning Don't use EnsureCreated and migrations to manage the same database schema.

    Add and Query Data

    Replace the contents of Program.cs with the following code. The example initializes the database schema, adds two products if the table is empty, and retrieves products with a price below 500:

    using (var context = new ShopContext()) {
      context.Database.EnsureCreated();
    
      if (!context.Products.Any()) {
        context.Products.Add(new Product { Name = "Laptop", Price = 999.99m });
        context.Products.Add(new Product { Name = "Mouse", Price = 19.90m });
        context.SaveChanges();
      }
    
      var products = context.Products
        .Where(p => p.Price < 500)
        .OrderBy(p => p.Name)
        .ToList();
    
      foreach (var product in products)
        Console.WriteLine($"{product.Id}: {product.Name}, {product.Price}");
    }
    

    Run the application with the dotnet run command. The application prints output similar to the following:

    2: Mouse, 19.90
    

    Entity Framework Core assigns the key values from the sequence before the rows are inserted, so the generated key values might differ. For more information, see Generating Key Values.

    dotConnect for Snowflake encloses the names of tables and columns in double quotation marks, so they are case-sensitive. To query the table in Snowsight or another tool, use double quotation marks and match the original case of the names:

    SELECT "Id", "Name", "Price" FROM "Products";
    

    For more information, see Names of Database Objects.

    Next Steps