UOW EXCEPTION ,MULTIPLE DATABSE AND MULTIPLE DBCONTEXT
See original GitHub issueMy application have two database . the one is sql and another one is oracle so .I add the second dbcontext ,in my application ,I want to use procedure or sqlcommand.I saw issue https://github.com/aspnetboilerplate/as … issues/574 https://github.com/aspnetboilerplate/as … issues/583 follow this : 1.I generate code from website ,and use module zero template . so ,the first main dbcontext is "webapidbcontext ". 2. install Oracle Data Provider for .NET (ODP.NET) Managed Driver, run the following command in the Package Manager Console:Install-Package odp.net.managed. config connectstring like this CODE: SELECT ALL <add name="Default" connectionString="Server=localhost; Database=WebApi; Trusted_Connection=True;" providerName="System.Data.SqlClient" /> <add name="His" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=QXYY;Password=qxyy;Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = TianDao-Pc)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = QXHIS) ))" />
- Then added second dbcontext
public class HisDbContext: AbpDbContext { //patient entity add public virtual DbSet<PATIENT> PATIENT { get; set; }
public HisDbContext()
: base("His")
{
}
public HisDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
public HisDbContext(DbConnection connection)
: base(connection, true)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("QXYY");
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
4.add patient entity : public class PATIENT:Entity<int> { public long PID { get; set; } public string NAME { get; set; } } 5.modify module: public override void Initialize() { IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); Database.SetInitializer<WebApiDbContext>(null); Database.SetInitializer<HisDbContext>(null); }
6.migration and updatebase: Enable-Migrations -MigrationsDirectory “MigrationsHis” -ContextTypeName “HisDbContext” Add-Migration -configuration TianDao.WebApi.MigrationsHis.Configuration inital Update-Database -ConfigurationTypeName “TianDao.WebApi.MigrationsHis.Configuration” 7.add service interface : public interface IHisAppService : IApplicationService { [HttpGet] List<PatientDto> AllPatientinfo(); } 8.implement service interface: public class HisAppService : ApplicationService, IHisAppService { private readonly IRepository<PATIENT> _patientRepository; public HisAppService(IRepository<PATIENT> patientRepository) { _patientRepository = patientRepository; }
public List<PatientDto> AllPatientinfo() { var p = _patientRepository.GetAllList(); var p2 = p.MapTo<List<PatientDto>>(); return p2; } } 9.finaly,after login ,I access this URL:http://localhost:6234/api/services/webapi/his/AllPatientinfo from browser. but throw Exception.I saw log ,find this :System.Data.SqlClient.SqlException: object name ‘QXYY.PATIENT’ is invalid .I find this is a uow exception ,in HisAppService allpatientinfo method current dbcontext is webapicontext ,not hisdbcontext . why? i think this error should not Happen.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top GitHub Comments
OK,beause the second dbcontext constructor .delete this code
"public HisDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public HisDbContext(DbConnection connection) : base(connection, true) { } "
@tiandao-dongguan 方便留一下联系方式么?感觉我们做的差不多,我是新手,想跟您请教一下,可能会耽误您一点时间,谢谢。