Base solution for your next web application
Open Closed

How to disable transactions? #2194


User avatar
0
carelearning created

Hello,

When we try to create a database like this:

[UnitOfWork(false)]
        public bool Create()
        {
            DatabaseUpgradeResult result;
            try
            {
                EnsureDatabase.For.SqlDatabase(ConnectionString);//fails here with error below

                var upgrader =
                    DeployChanges.To
                        .SqlDatabase(ConnectionString)
                        .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                        .LogToConsole()
                        .Build();

                result = upgrader.PerformUpgrade();
            }
            catch (Exception exception)
            {
                throw;
            }
            return result.Successful;
        }

This throws the error: CREATE DATABASE statement not allowed within multi-statement transaction.

We see the post here [http://forum.aspnetboilerplate.com/viewtopic.php?f=5&t=4103&p=9404]) and the article here [http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowNoTransaction]). We tried to follow these instructions by converting our our class library to a console application and setting Program.cs as the startup class:

namespace MyCompanyName.AbpZeroTemplate.LegacyDatabaseService
{
    using Abp;

    public class Program
    {
        public static void Main(string[] args)
        {
            using (var bootstrapper = AbpBootstrapper.Create<AbpZeroTemplateLegacyDatabaseServiceModule>())
            {
                bootstrapper.Initialize();
            }
        }

    }
}

We then created AbpZeroTemplateLegacyDatabaseServiceModule.cs as a module to set the initializer to null like the migrator project :

namespace MyCompanyName.AbpZeroTemplate.LegacyDatabaseService
{
    using System.Data.Entity;
    using Abp.Modules;
    using System.Reflection;
    using EntityFramework;
    
    [DependsOn(typeof(AbpZeroTemplateDataModule))]
    public class AbpZeroTemplateLegacyDatabaseServiceModule: AbpModule
    {
        public override void PreInitialize()
        {
            Database.SetInitializer<AbpZeroTemplateTenantDbContext>(null);
        }

        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
        }
    }
}

We also tried to turn off transactions by using: [UnitOfWork(isTransactional: false)] but the error still occurs.

Is this the correct way to turn off transactions?

Or is there another possible solution for this error?

Thank you


1 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Disabling unitOfWork that way should work but there is something else I think. How do you call Create method in Program.cs file ? I couldn't see it.

    Alternatively, you can disable all transactions like this in your initialize method of your AbpZeroTemplateLegacyDatabaseServiceModule class.

    Configuration.UnitOfWork.IsTransactional = false;