Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

DbContext overriding default when plugins added #6613


User avatar
0
terry21 created

I have a ASPNet Zero Core application that works fine until I add an application dlls into Plugins Folder. My application I am plugging in is using the same architecture as the main boilerplate (i.e. Application, Core, EntityFramework, Web.Core,...) with only the application specific entities (Abp entities removed) As soon as I drop the dlls into the Plugins Folder and start the server side application I get an error that shows as an error in the Log as "Invalid object name 'AbpLanguages'" As a test, I added an AbpLanguages table to the Plugins application database and the program then complained about another invalid object; adding that table that was referenced and the program went further again, and so on. Obviously the Plugin database DbContext (SampleDbContext) is overriding the Default DbContext. Any ideas on how this is happening and fixing it would be appreciated! Thanks

On the Plugin code:

    public class SampleDbContext : DbContext
    {
        public virtual DbSet<SampleItem> SampleItems { get; set; }

        public SampleDbContext(DbContextOptions<SampleDbContext> options) 
                : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.ConfigurePersistedGrantEntity();
        }
    }
-----------------------------------------------
        public static class SampleDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<SampleDbContext> builder, string connectionString)
        {
            builder.UseSqlServer(connectionString);
        }

        public static void Configure(DbContextOptionsBuilder<SampleDbContext> builder, DbConnection connection)
        {
            builder.UseSqlServer(connection);
        }
    }

WebCoreModule PreInitialize


Configuration.DefaultNameOrConnectionString = _appConfiguration.GetConnectionString(SampleConst.ConnectionStringName);


4 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi, please share your ANZ project version and framework (e.g. .net core & angular 6.7.0)

    also, can you provide the full stack trace for the exception Invalid object name 'AbpLanguages'

  • User Avatar
    0
    terry21 created

    I am running Abp.AspNetCore 4.3.0. This is happening on the server-side before even starting the angular app. It looks like any Connection string that I add to the main calling Web.Host appsetting.json file overrides my "Default" connection string.

    In Web.Host\Startup\Startup.cs Configure method:

                app.UseStaticFiles();
    
                using (var scope = app.ApplicationServices.CreateScope())
                {
                    if (scope.ServiceProvider.GetService<DatabaseCheckHelper>().Exist(_appConfiguration["ConnectionStrings:Default"]))
                    {
                        app.UseAbpRequestLocalization();
                    }
                }
    

    the_ appConfiguration["ConnectionStrings:Default"]__ value is correct - it points to my Abp database but the connection string used in the app.UseAbpRequestLocalization(); is using the non-Abp connection string as it is connecting to my non-Abp database and so cannot find AbpLanguages (see this using SQL Server Profiler)

    I debugged the DatabaseCheckHelper method and when I have the Pludin dlls in the Plugin folder it tries to open the connection but produces the exception Invalid value for key 'attachdbfilename' If I take out the dlls from the Plugins folder the program runs using the correct Abp database.

    Ideally I would like to specify the connection string for my plugin within the plugin code but have not figured out how to do that as there is no Startup in the plugin.

  • User Avatar
    0
    ryancyq created
    Support Team

    are you loading second dbcontext from the plugin module?

    you can try with multiple dbcontext example to solve your connection string issue

    see https://github.com/aspnetboilerplate/aspnetboilerplate-samples/tree/master/MultipleDbContextEfCoreDemo

  • User Avatar
    0
    terry21 created

    Thanks for the suggestion. I had found the MultipleDbContextEfCoreDemo and was using it to go by but in my case I am trying to set the second dbcontext from the plugin. However, instead of adding a second dbcontext in the plugin I am somehow overriding the primary dbcontext.

    I have been able to resolve the issue with the "invalid value for key 'attachdbfilename' issue though. I thought that was the source of the problem but it wasn't.