How to connect two databases?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
I have two databases now. One is nopCommerce's database, another is mine. Because I have some special tables and it is used by another website.

How can I access two databases in the same time?

Thanks.

Version: 4.10
5 years ago
tzhsu wrote:
I have two databases now. One is nopCommerce's database, another is mine. Because I have some special tables and it is used by another website.

How can I access two databases in the same time?

Thanks.

Version: 4.10


Please use SQL link server
https://www.sqlshack.com/how-to-create-and-configure-a-linked-server-in-sql-server-management-studio/
5 years ago
i have done it for the 4.0 version

data project
create a additional  ObjectContext
include new table maps in the OnModelCraeting
add a EFStartUpTask (new namespace) and execute  Database.SetInitializer<NewObjectContext>(null);

presentation
add a new dependencyRegister
add add data layer

add a new settings.json file name it and change the connection string


#region Data

            ////data layer
            var DataSettingsFilePath = "~/App_Data/windalert.json";
            DataSettingsFilePath = DataSettingsFilePath.Replace("~/", "").TrimStart('/').Replace('/', '\\');

            //var fileProvider = EngineContext.Current.Resolve<INopFileProvider>();

            //var windalertFilePath = fileProvider.MapPath(DataSettingsFilePath);

            //data layer
            var dataSettingsManager = new DataSettingsManager();
            var dataProviderSettings = dataSettingsManager.LoadSettings(DataSettingsFilePath, true);

            if (dataProviderSettings != null && dataProviderSettings.IsValid())
            {
                //register named context
                //register named context
                builder.Register<IDbContext>(c => new WindAlertObjectContext(dataProviderSettings.DataConnectionString))
                    .Named<IDbContext>("nop_object_context_windalert")
                    .InstancePerLifetimeScope();

                builder.Register(c => new WindAlertObjectContext(dataProviderSettings.DataConnectionString))
                    .InstancePerLifetimeScope();
            }
            else
            {
                //register named context
                builder.Register<IDbContext>(c => new WindAlertObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                   .Named<IDbContext>("nop_object_context_windalert")
                   .InstancePerLifetimeScope();

                builder.Register(c => new WindAlertObjectContext(c.Resolve<DataSettings>().DataConnectionString))
                    .InstancePerLifetimeScope();
            }

            //override required repository with our custom context
            builder.RegisterType<EfRepository<WindData>>()
                .As<IRepository<WindData>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
                .InstancePerLifetimeScope();
            builder.RegisterType<EfRepository<WaveData>>()
                .As<IRepository<WaveData>>()
                .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
                .InstancePerLifetimeScope();
            builder.RegisterType<EfRepository<ForecastData>>()
               .As<IRepository<ForecastData>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
               .InstancePerLifetimeScope();
            builder.RegisterType<EfRepository<MagicData>>()
               .As<IRepository<MagicData>>()
               .WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_windalert"))
               .InstancePerLifetimeScope();
        #endregion
5 years ago
Thank you for sharing suolution.

This will help to community.
5 years ago
Thanks guys! I'll try them.
5 years ago
Is there a way to register EfRepository dynamically instead of having to specify one by one?
5 years ago
Hello ,

I am integrating my windows application with nopcommerce. So In that when I register the customer from nopcommerce then all the info should be saved to new table 'Customer' of another database of my windows app. for that I created trigger after insert on Table GenericAttribute.

But when I am going to register customer then error occurs in following schema at EfRepository.cs :

protected string GetFullErrorTextAndRollbackEntityChanges(DbUpdateException exception)
        {
            //rollback entity changes
            if (_context is DbContext dbContext)
            {
                var entries = dbContext.ChangeTracker.Entries()
                    .Where(e => e.State == EntityState.Added || e.State == EntityState.Modified).ToList();

                entries.ForEach(entry => entry.State = EntityState.Unchanged);//here error occurs
            }

            _context.SaveChanges();
            return exception.ToString();
        }

and the Error is :

InvalidOperationException: The property 'Id' on entity type 'GenericAttribute' has a temporary value while attempting to change the entity's state to 'Unchanged'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property

What should I do.....???
2 years ago
Thanks for the solution above - worked well for v. 4.20.  But how to do it in v 4.40?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.