Send feedback on this topic.
Teradata.Client.Provider
Example of how to use the EFCore Provider
.NET Data Provider for Teradata > Developer's Guide > Teradata Entity Framework Core Provider > Example of how to use the EFCore Provider
.NET Framework  This feature is not supported by the .NET Framework implementation of the Data Provider.

The following is an example of how to connect to a Teradata SQL Engine and create, query, and delete an entity using the EFCore Provider.

EFCore Sample App
Copy Code
using System;
using Microsoft.EntityFrameworkCore;

namespace Teradata.EntityFrameworkCore.Example
{
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    }

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // TODO: set appropriate ConnectionString options for your Data Source
            // Note: the Default Database is required.
            optionsBuilder.UseTeradata("Data Source=MYTERADATADATABASE;" +
                "User Id=BlogUser;Password=BlogPassword;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // Required due to a Modeling limitation
            modelBuilder.Entity<Blog>().Property(e => e.Url).HasColumnType("VARCHAR(2048)");
            
            // TODO: Annotate the model with desired database object properties
            modelBuilder.ForTeradataUseStorageOptions("PERM = 1E7*(HASHAMP()+1)");
        }
    }

    public class Program
    {
        public static void Main()
        {
            using (var db = new BloggingContext())
            {
                // TODO: use Migrations or db.Database.EnsureCreated() to create database and tables
                db.Database.EnsureCreated();

                Blog[] localData = new[]
                {
                    new Blog { Url = "https://blogs1.example.com" },
                    new Blog { Url = "https://blogs2.example.com" },
                };

                db.Blogs.AddRange(localData);
                Console.WriteLine($"{db.SaveChanges()} records saved to database.\n");

                Console.WriteLine("Local data after SaveChanges():");
                foreach (Blog blog in localData)
                {
                    Console.WriteLine($"  {blog.BlogId} - {blog.Url}");
                }

                db.Blogs.RemoveRange(localData);
                Console.WriteLine($"\n{db.SaveChanges()} records removed from database.");
            }
        }
    }
}

See Also

Limitations