protected static FR_Base Execute(DbConnection Connection, DbTransaction Transaction, P_PA_ACtP_1452 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            #region UserCode
            var returnValue = new FR_Base();
            //Put your code here
            var consentsGrouped = Parameter.consents.GroupBy(t => t.insurance_id).Select(t => new { key = t.Key, value = t }).ToDictionary(t => t.key, t => t.value);

            var iviContract = ORM_CMN_CTR_Contract.Query.Search(Connection, Transaction, new ORM_CMN_CTR_Contract.Query()
            {
                Tenant_RefID = securityTicket.TenantID, IsDeleted = false, ContractName = "IVI-Vertrag"
            }).SingleOrDefault();
            if (iviContract != null)
            {
                var iviInsuranceToBrokerContract = ORM_HEC_CRT_InsuranceToBrokerContract.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract.Query()
                {
                    Tenant_RefID = securityTicket.TenantID,
                    IsDeleted    = false,
                    Ext_CMN_CTR_Contract_RefID = iviContract.CMN_CTR_ContractID
                }).SingleOrDefault();
                if (iviInsuranceToBrokerContract != null)
                {
                    foreach (var consent in consentsGrouped)
                    {
                        foreach (var data in consent.Value)
                        {
                            var patient = cls_Get_PatientID_for_PatientInsuranceID_and_PracticeBSNR.Invoke(Connection, Transaction, new P_PA_GPIDfPIIDaPBSNR_1552()
                            {
                                BSNR        = data.bsnr,
                                InsuranceID = data.insurance_id
                            }, securityTicket).Result;

                            if (patient != null)
                            {
                                var patient_consents = ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingPatient.Query.Search(Connection, Transaction, new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingPatient.Query()
                                {
                                    Tenant_RefID  = securityTicket.TenantID,
                                    IsDeleted     = false,
                                    Patient_RefID = patient.PatientID
                                });

                                #region delete existing consents
                                foreach (var existing_consent in patient_consents)
                                {
                                    existing_consent.Modification_Timestamp = DateTime.Now;
                                    existing_consent.IsDeleted = true;

                                    existing_consent.Save(Connection, Transaction);
                                }

                                #endregion

                                #region add new consents

                                foreach (var newConsent in consent.Value)
                                {
                                    var newParticipationConsent = new ORM_HEC_CRT_InsuranceToBrokerContract_ParticipatingPatient();
                                    newParticipationConsent.InsuranceToBrokerContract_RefID = iviInsuranceToBrokerContract.HEC_CRT_InsuranceToBrokerContractID;
                                    newParticipationConsent.Modification_Timestamp          = DateTime.Now;
                                    newParticipationConsent.Patient_RefID = patient.PatientID;
                                    newParticipationConsent.Tenant_RefID  = securityTicket.TenantID;
                                    newParticipationConsent.ValidFrom     = newConsent.start_date;

                                    newParticipationConsent.Save(Connection, Transaction);
                                }
                                #endregion
                            }
                            else
                            {
                                throw new Exception("No patient found for insurance id: " + data.insurance_id + " and bsnr: " + data.bsnr);
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception("IVI-Vertrag insurance to broker contract not found.");
                }
            }
            else
            {
                throw new Exception("IVI-Vertrag contract not found.");
            }
            return(returnValue);

            #endregion UserCode
        }
 ///<summary>
 /// Invokes the method for the given Connection, and Transaction, leaving them open/not commited if no exceptions occured
 ///<summary>
 public static FR_Base Invoke(DbConnection Connection, DbTransaction Transaction, P_PA_ACtP_1452 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(Connection, Transaction, null, Parameter, securityTicket));
 }
        ///<summary>
        /// Method Invocation of wrapper classes
        ///<summary>
        protected static FR_Base Invoke(DbConnection Connection, DbTransaction Transaction, string ConnectionString, P_PA_ACtP_1452 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
        {
            bool cleanupConnection  = Connection == null;
            bool cleanupTransaction = Transaction == null;

            FR_Base functionReturn = new FR_Base();

            try
            {
                if (cleanupConnection == true)
                {
                    Connection = CSV2Core_MySQL.Support.DBSQLSupport.CreateConnection(ConnectionString);
                    Connection.Open();
                }
                if (cleanupTransaction == true)
                {
                    Transaction = Connection.BeginTransaction();
                }

                functionReturn = Execute(Connection, Transaction, Parameter, securityTicket);

                #region Cleanup Connection/Transaction
                //Commit the transaction
                if (cleanupTransaction == true)
                {
                    Transaction.Commit();
                }
                //Close the connection
                if (cleanupConnection == true)
                {
                    Connection.Close();
                }
                #endregion
            }
            catch (Exception ex)
            {
                try
                {
                    if (cleanupTransaction == true && Transaction != null)
                    {
                        Transaction.Rollback();
                    }
                }
                catch { }

                try
                {
                    if (cleanupConnection == true && Connection != null)
                    {
                        Connection.Close();
                    }
                }
                catch { }

                throw new Exception("Exception occured in method cls_Add_Consents_to_Patients", ex);
            }
            return(functionReturn);
        }
 ///<summary>
 /// Opens the connection/transaction for the given connectionString, and closes them when complete
 ///<summary>
 public static FR_Base Invoke(string ConnectionString, P_PA_ACtP_1452 Parameter, CSV2Core.SessionSecurity.SessionSecurityTicket securityTicket = null)
 {
     return(Invoke(null, null, ConnectionString, Parameter, securityTicket));
 }