Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EfCoreTransactionStrategy to share transactions #1834

Merged
merged 5 commits into from Mar 3, 2017
Merged

Add EfCoreTransactionStrategy to share transactions #1834

merged 5 commits into from Mar 3, 2017

Conversation

diogodamiani
Copy link
Contributor

@diogodamiani diogodamiani commented Feb 4, 2017

Hi @hikalkan,

I've implemented the EfCoreTransactionStrategy based on EfTransactionStrategy. It fix #1829.

One thing I don't know if it's good enough: The connection is propagated through AbpDbContextConfiguration. At Startup we should do the following:

services.AddAbpDbContext<MyDbContext>(options => { 
    if (options.Connection == null)
        DbContextOptionsConfigurer.Configure(
            options.DbContextOptions, options.ConnectionString);
    else
        DbContextOptionsConfigurer.Configure(
            options.DbContextOptions, options.Connection);
});

The Configure method should have an overload for DbConnection:

public static class DbContextOptionsConfigurer
{
    public static void Configure(
        DbContextOptionsBuilder<SimpleTaskAppDbContext> dbContextOptions, 
        string connectionString
        )
    {
        /* This is the single point to configure DbContextOptions for MyDbContext */
        dbContextOptions.UseSqlServer(connectionString);
    }

    public static void Configure(
        DbContextOptionsBuilder<SimpleTaskAppDbContext> dbContextOptions, 
        DbConnection connection
        )
    {
        /* This is the single point to configure DbContextOptions for MyDbContext */
        dbContextOptions.UseSqlServer(connection);
    }
}

I'll appreciate your review. Thank you!

P.S.: I would like to thank @alexandrejose for the collaboration in the development! :)

@diogodamiani diogodamiani changed the title EFCore SharedTransaction Fix #1829 Add EfCoreTransactionStrategy to share transactions Feb 4, 2017
@hikalkan hikalkan self-requested a review February 4, 2017 19:06
@hikalkan hikalkan added this to the v1.5.0 milestone Feb 4, 2017
@hikalkan
Copy link
Member

hikalkan commented Feb 4, 2017

Thanks a lot. I will review it.

@hikalkan hikalkan merged commit 817b409 into aspnetboilerplate:dev Mar 3, 2017
@hikalkan
Copy link
Member

hikalkan commented Mar 3, 2017

Thank you very much. It's well designed. I merged and will be released with v1.5.0. I will continue to review it and will refactor if needed.

@hikalkan
Copy link
Member

hikalkan commented Mar 3, 2017

I will make a few changes if it works: #1909

@hikalkan
Copy link
Member

hikalkan commented Mar 3, 2017

So, it's better to check ExistingConnection before (renamed from Connection):

            Configuration.Modules.AbpEfCore().AddDbContext<MyDbContext>(options =>
            {
                if (options.ExistingConnection != null)
                {
                    options.DbContextOptions.UseSqlServer(options.ExistingConnection);
                }
                else
                {
                    options.DbContextOptions.UseSqlServer(options.ConnectionString);
                }
            });

@brunobertechini
Copy link

@hikalkan One thing with given solution is :

options.DbContextOptions.UseSqlServer(options.ConnectionString);

Always return the "Default" conn string. Is there any way to register my custom dbcontext conn string different from Default ?

Right now I am using the following approach:

IHostingEnvironment env = IocManager.Resolve<IHostingEnvironment>();
var connString = env.GetAppConfiguration().GetConnectionString(BusinessIntelligenceConsts.ConnectionStringName);
BusinessIntelligenceDbContextConfigurer.Configure(configuration.DbContextOptions, connString);

And now i will try to apply the given solution and try to resolve #2325

@acjh
Copy link
Contributor

acjh commented Aug 30, 2017

Is there any way to register my custom dbcontext conn string different from Default ?

See https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core:

So, where to set default connection string?

In Module PreInitialize

You can do it in PreInitialize of your module as shown below:

public class MyEfCoreAppModule : AbpModule
{
    public override void PreInitialize()
    {
        Configuration.DefaultNameOrConnectionString = GetConnectionString("Default");
        ...
    }
}

So, you can define GetConnectionString method simply returns the connection string from a configuration file (generally from appsettings.json).

@brunobertechini
Copy link

@acjh This will work for main app conn string.

I have 5 modules each with its own conn string.

Or are you suggesting extending the configuration for each module ?

@hikalkan
Copy link
Member

Is there any way to register my custom dbcontext conn string different from Default?

You should implement and replace IConnectionStringResolver. In this custom service you can check dbcontext type (or other logic) to determine connection string dynamically.

DbPerTenantConnectionStringResolver (https://github.com/aspnetboilerplate/module-zero/blob/dev/src/Abp.ZeroCore.EntityFrameworkCore/Zero/EntityFrameworkCore/DbPerTenantConnectionStringResolver.cs) changes it to dynamically determine connection string per tenant.

hikalkan added a commit to aspnetboilerplate/module-zero-core-template that referenced this pull request Sep 7, 2017
renawolford6 added a commit to renawolford6/module-core-template-Asp.net that referenced this pull request Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error sharing transaction among EF Core contexts
4 participants