version:nopcommerce3.3
I want to connect two databases in the nopcommerce。
Can this function been done?
Thanks!
using System;
namespace Nop.Core.Domain.AnyThing
{
/// <summary>
/// Represents a
/// </summary>
public partial class yourTable : BaseEntity
{
/// <summary>
/// Gets or sets the name
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// Gets or sets the date and time of instance creation
/// </summary>
public virtual DateTime CreatedOnUtc { get; set; }
}
}
using Nop.Core.Domain.AnyThing;
namespace Nop.Data.Mapping.AnyThing
{
public partial class yourTableMap : NopEntityTypeConfiguration<yourTable>
{
public yourTableMap()
{
this.ToTable("TableName");
this.HasKey(fd => fd.Id);
}
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Reflection;
using Nop.Core;
using Nop.Data.Mapping.AnyThing;
namespace Nop.Data.AnyThing
{
/// <summary>
/// Object context
/// </summary>
public class AnyThingObjectContext : DbContext, IDbContext
{
#region Ctor
public AnyThingObjectContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
//((IObjectContextAdapter) this).ObjectContext.ContextOptions.LazyLoadingEnabled = true;
}
#endregion
#region Utilities
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new yourTableMap());
//disable EdmMetadata generation
//modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
base.OnModelCreating(modelBuilder);
}
#endregion
#region Methods
public string CreateDatabaseScript()
{
return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
}
public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
{
return base.Set<TEntity>();
}
public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
{
throw new NotImplementedException();
}
public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
{
throw new NotImplementedException();
}
/// <summary>
/// Executes the given DDL/DML command against the database.
/// </summary>
/// <param name="sql">The command string</param>
/// <param name="timeout">Timeout value, in seconds. A null value indicates that the default value of the underlying provider will be used</param>
/// <param name="parameters">The parameters to apply to the command string.</param>
/// <returns>The result returned by the database after executing the command.</returns>
public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
{
throw new NotImplementedException();
}
#endregion
}
}
using Nop.Core;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using System.Data.Entity;
namespace Nop.Data.AnyThing
{
public class EfStartUpTask : IStartupTask
{
public void Execute()
{
//It's required to set initializer to null (for SQL Server Compact).
//otherwise, you'll get something like "The model backing the 'your context name' context has changed since the database was created. Consider using Code First Migrations to update the database"
Database.SetInitializer<AnyThingObjectContext>(null);
}
public int Order
{
//ensure that this task is run first
get { return -1000; }
}
}
}
using Autofac;
using Autofac.Core;
using Autofac.Integration.Mvc;
using Nop.Core.Data;
using Nop.Core.Domain.AnyThing;
using Nop.Core.Infrastructure;
using Nop.Core.Infrastructure.DependencyManagement;
using Nop.Data;
using Nop.Data.AnyThing;
using System;
using System.IO;
using System.Web.Hosting;
namespace Nop.Web.AnyThing
{
public class DependencyRegistrar : IDependencyRegistrar
{
protected virtual string MapPath(string path)
{
if (HostingEnvironment.IsHosted)
{
//hosted
return HostingEnvironment.MapPath(path);
}
else
{
//not hosted. For example, run in unit tests
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');
return Path.Combine(baseDirectory, path);
}
}
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
#region Data
//data layer
var dataSettingsManager = new DataSettingsManager();
var dataProviderSettings = dataSettingsManager.LoadSettings(Path.Combine(MapPath("~/App_Data/"), "AnyThing.txt"));
if (dataProviderSettings != null && dataProviderSettings.IsValid())
{
//register named context
builder.Register<IDbContext>(c => new AnyThingObjectContext(dataProviderSettings.DataConnectionString))
.Named<IDbContext>("nop_object_context_anything")
.InstancePerHttpRequest();
builder.Register<AnyThingObjectContext>(c => new AnyThingObjectContext(dataProviderSettings.DataConnectionString))
.InstancePerHttpRequest();
}
else
{
//register named context
builder.Register<IDbContext>(c => new AnyThingObjectContext(c.Resolve<DataSettings>().DataConnectionString))
.Named<IDbContext>("nop_object_context_anything")
.InstancePerHttpRequest();
builder.Register<AnyThingObjectContext>(c => new AnyThingObjectContext(c.Resolve<DataSettings>().DataConnectionString))
.InstancePerHttpRequest();
}
//override required repository with our custom context
builder.RegisterType<EfRepository<yourTable>>()
.As<IRepository<yourTable>>()
.WithParameter(ResolvedParameter.ForNamed<IDbContext>("nop_object_context_anything"))
.InstancePerHttpRequest();
#endregion
}
public int Order
{
get { return 1; }
}
}
}
DataProvider: sqlserver
DataConnectionString: Data Source=ServerName;Initial Catalog=DataBaseName;Integrated Security=True;Persist Security Info=False;