Exemplo n.º 1
0
        private int ReadPrimaryKeys(SqlCommand cmd, EntityTable table, string columnName)
        {
            int primaryKeyIndex = 0;

            Logger.Debug($"Reading primary keys inserted into {table.Name}");
            Logger.Debug($"Executing SQL: {cmd.CommandText}");

            using SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                dynamic primaryKey = reader[columnName];

                Logger.Trace($"Inserted '{table.Type.Name}' entity with primary key {primaryKey.ToString()}");

                /*
                 * We need to make sure that the data rows for child entities which have a foreign key relationship
                 * to this newly inserted entity are updated with foreign keys. These are currently set to the default
                 * value of their data type, and so will all be the same - any insert operation attempted at this point
                 * would cause a foreign key exception in SQL.
                 */

                table.UpdatePrimaryKey(primaryKey, primaryKeyIndex);
                table.UpdateForeignKeys(primaryKey, primaryKeyIndex++);
            }

            return(primaryKeyIndex);
        }