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

uow exception ,multiple databse and multiple dbcontext #371


User avatar
0
tiandao created

My 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 <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/574">https://github.com/aspnetboilerplate/as ... issues/574</a> <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/583">https://github.com/aspnetboilerplate/as ... issues/583</a> 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

<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) ))" />
  1. Then added second dbcontext :
public class HisDbContext: AbpDbContext
    {

       //patient entity
        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&lt;PatientDto&gt; AllPatientinfo()
    {
            var p = _patientRepository.GetAllList();
            var p2 = p.MapTo&lt;List&lt;PatientDto&gt;>();
            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.

can you give me solution? thank you . i want use procedure(hisdbcontext) in my application ,can you give me solution? thank you .thank you.thank you


3 Answer(s)
  • User Avatar
    0
    tiandao created

    I tried disable uow and use custom repository ,but my application throw exception ,not work correctly . this time ,I use sqlserver database and other database . 1.custom repository interface :

    public interface IPacsRepository:IRepository<Register>
        {
        List<string> Patientinfo();
        }
    

    2.implement :

    public class PacsRepository : PacsRepositoryBase<Register>,IPacsRepository
        {
          
            public PacsRepository(IDbContextProvider<PacsDbContext> dbContextProvider) : base(dbContextProvider)
            {
    
    
            }
    
            public List<string> Patientinfo()
            {
    
                var query = GetAllList().Select(p => p.BrName).ToList();
                return query;
            }
        }
    

    3.called in service :

    public class PacsAppService : ApplicationService,IPacsAppService
        {
    
            
            private readonly IPacsRepository _ppacsRepository; //custom repository 
            public PacsAppService(IPacsRepository ppacsRepository)
            {
                _ppacsRepository = ppacsRepository; //custom repository 
    
            }
    
    
            [UnitOfWork(IsDisabled=true)]
            public List<string> Patientinfo()
            {
                var register = _ppacsRepository.Patientinfo();
                return register;
    
            }
        }
    
  • User Avatar
    0
    hikalkan created
    Support Team

    Continue this from the issue: <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/issues/645">https://github.com/aspnetboilerplate/as ... issues/645</a> since it maybe a bug. Thanks.

  • User Avatar
    0
    tiandao created

    beause the second dbcontext constructor ,delete this code

    "public HisDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } public HisDbContext(DbConnection connection) : base(connection, true) { } "