DataConnector
ADO.NET provider for Kintone / Entity Framework
In This Topic
    Entity Framework
    In This Topic

    The ADO.NET Provider for Kintone supports Entity Framework which requires C1.EntityFrameworkCore.Kintone package to be installed. In this section, we have used the model first approach to build an Entity Framework model that maps data tables to classes for simpler access to Kintone.

    The below code example defines the Accounts class to map the Accounts datatable.

    C#
    Copy Code
    public partial class Accounts
    {
        public int Id { get; set; }
        public string Status { get; set; }
        public string Assignee { get; set; }
        public string AccountId { get; set; }
        public string Address { get; set; }
        public string Categories { get; set; }
        public DateTime? UpdatedDate { get; set; }
        public string Sex { get; set; }
        public DateTime? CreatedDate { get; set; }
        public string UpdatedBy { get; set; }
        public string Name { get; set; }
        public int? RecordNumber { get; set; }
        public string CreatedBy { get; set; }
        public double? Age { get; set; }
        public DateTime? BirthDate { get; set; }
    

    Now, KintoneContext class has been defined to access the Accounts datatable and establish a connection to the Kintone service by overriding the OnConfiguring method. This method invokes the UseKintone method of DbContextOptionsBuilder class to configure the context and establish connection with Kintone service.

    C#
    Copy Code
    public partial class KintoneContext : DbContext
    {
        public KintoneContext()
        {
            Database.AutoTransactionsEnabled = false;
        }
    
        public KintoneContext(DbContextOptions<KintoneContext> options)
            : base(options)
        {
            Database.AutoTransactionsEnabled = false;
        }
    
        public virtual DbSet<Accounts> Accounts { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseKintone("Username=****;Password=*****;Url=*****");
            }
    

    You can now use the LINQ queries to perform different data operations to the mapped classes as demonstrated in the code below. Note that "using System.Linq" must be declared in your code.

    C#
    Copy Code
    public static void Select(KintoneContext context)
    {
        Console.WriteLine("Query all Accounts...");
        var records =
            from p in context.Accounts
            select p;
    
        foreach (var account in records)
        {
            Console.WriteLine("{0} - {1} - {2} - {3} - {4} - {5} - {6}",
                               account.RecordNumber, account.Name, account.Address, account.Sex, account.BirthDate, account.CreatedBy, account.CreatedDate);
        }
    }
    
    public static void Insert(KintoneContext context)
    {
        Console.WriteLine("\nQuery Insert...");
        Accounts account = new Accounts
        {
            Name = "Account Insert " + DateTime.Now.Ticks.ToString(),
            AccountId = "116",
            Sex = "Male"
        };
    
        context.Accounts.Add(account);
    
        int result = context.SaveChanges();
        Console.WriteLine("Number row insert: " + result);
    }
    
    public static void Update(KintoneContext context)
    {
        Console.WriteLine("\nQuery Update...");
        var accounts = context.Accounts.Where(x => x.AccountId == "116");
    
        foreach (var account in accounts)
        {
            account.Age = 24;
            account.Address = "London, UK";
        }
    
        int result = context.SaveChanges();
    
        Console.WriteLine("Number row update: " + result);
    }
    
    public static void Delete(KintoneContext context)
    {
        Console.WriteLine("\nQuery Delete...");
        var accounts = context.Accounts.Where(x => x.AccountId == "116");
    
        foreach (var account in accounts)
        {
            context.Accounts.Remove(account);
        }
    
        int result = context.SaveChanges();
    
        Console.WriteLine("Number row delete: " + result);
    }
    
    Note: Scaffolding feature supports user to create the model and dbcontext when you create model in the Entity Framework for all dataconnectors.