<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace EFCore.BulkExtensions
{
    public class TableInfo
    {
        public string Schema { get; set; }
        public string SchemaFormated => Schema != null ? Schema + "." : "";
        public string Name { get; set; }
        public string FullTableName => $"{SchemaFormated}[{Name}]";
        public string PrimaryKey { get; set; }
        public string PrimaryKeyFormated => $"[{PrimaryKey}]";

        public string TempTableSufix { get; set; }
        public string FullTempTableName => $"{SchemaFormated}[{Name}{TempTableSufix}]";
        public string FullTempOutputTableName => $"{SchemaFormated}[{Name}{TempTableSufix}Output]";

        public bool InsertToTempTable { get; set; }
        public bool HasIdentity { get; set; }
        public int NumberOfEntities { get; set; }
        public BulkConfig BulkConfig { get; set; }
        public Dictionary<string, string> PropertyColumnNamesDict = new Dictionary<string, string>();

        public void LoadData<T>(DbContext context, bool loadOnlyPKColumn)
        {
            var entityType = context.Model.FindEntityType(typeof(T));
            if (entityType == null)
                throw new InvalidOperationException("DbContext does not contain EntitySet for Type: " + typeof(T).Name);

            var relationalData = entityType.Relational();
            Schema = relationalData.Schema;
            Name = relationalData.TableName;
            TempTableSufix = Guid.NewGuid().ToString().Substring(0, 8); // 8 chars of Guid as tableNameSufix to avoid same name collision with other tables

            PrimaryKey = entityType.FindPrimaryKey().Properties.First().Name;

            if (loadOnlyPKColumn)
            {
                PropertyColumnNamesDict.Add(PrimaryKey, PrimaryKey);
            }
            else
            {
                PropertyColumnNamesDict = entityType.GetProperties().ToDictionary(a => a.Name, b => b.Relational().ColumnName);
            }
        }

        public void CheckHasIdentity(DbContext context)
        {
            int hasIdentity = 0;
            var conn = context.Database.GetDbConnection();
            try
            {
                conn.OpenAsync();
                using (var command = conn.CreateCommand())
                {
                    string query = SqlQueryBuilder.SelectIsIdentity(FullTableName, PrimaryKey);
                    command.CommandText = query;
                    DbDataReader reader = command.ExecuteReader();<commit_msg>EFCore.BulkExtensions.TableInfo.CheckHasIdentity - use conn.Open() instead of conn.OpenAsync()<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace EFCore.BulkExtensions
{
    public class TableInfo
    {
        public string Schema { get; set; }
        public string SchemaFormated => Schema != null ? Schema + "." : "";
        public string Name { get; set; }
        public string FullTableName => $"{SchemaFormated}[{Name}]";
        public string PrimaryKey { get; set; }
        public string PrimaryKeyFormated => $"[{PrimaryKey}]";

        public string TempTableSufix { get; set; }
        public string FullTempTableName => $"{SchemaFormated}[{Name}{TempTableSufix}]";
        public string FullTempOutputTableName => $"{SchemaFormated}[{Name}{TempTableSufix}Output]";

        public bool InsertToTempTable { get; set; }
        public bool HasIdentity { get; set; }
        public int NumberOfEntities { get; set; }
        public BulkConfig BulkConfig { get; set; }
        public Dictionary<string, string> PropertyColumnNamesDict = new Dictionary<string, string>();

        public void LoadData<T>(DbContext context, bool loadOnlyPKColumn)
        {
            var entityType = context.Model.FindEntityType(typeof(T));
            if (entityType == null)
                throw new InvalidOperationException("DbContext does not contain EntitySet for Type: " + typeof(T).Name);

            var relationalData = entityType.Relational();
            Schema = relationalData.Schema;
            Name = relationalData.TableName;
            TempTableSufix = Guid.NewGuid().ToString().Substring(0, 8); // 8 chars of Guid as tableNameSufix to avoid same name collision with other tables

            PrimaryKey = entityType.FindPrimaryKey().Properties.First().Name;

            if (loadOnlyPKColumn)
            {
                PropertyColumnNamesDict.Add(PrimaryKey, PrimaryKey);
            }
            else
            {
                PropertyColumnNamesDict = entityType.GetProperties().ToDictionary(a => a.Name, b => b.Relational().ColumnName);
            }
        }

        public void CheckHasIdentity(DbContext context)
        {
            int hasIdentity = 0;
            var conn = context.Database.GetDbConnection();
            try
            {
                conn.Open();
                using (var command = conn.CreateCommand())
                {
                    string query = SqlQueryBuilder.SelectIsIdentity(FullTableName, PrimaryKey);
                    command.CommandText = query;
                    DbDataReader reader = command.ExecuteReader();
{'fraction_non_alphanumeric': 0.09681050656660413, 'fraction_numerical': 0.00150093808630394, 'mean_word_length': 2.354940213971051, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2743820', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1305, 'n_words': 338}