question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UOW EXCEPTION ,MULTIPLE DATABSE AND MULTIPLE DBCONTEXT

See original GitHub issue

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 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) ))" />

  1. 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tiandao-dongguancommented, Sep 29, 2015

OK,beause the second dbcontext constructor .delete this code

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

0reactions
ChaosFuuurycommented, Jan 20, 2017

@tiandao-dongguan 方便留一下联系方式么?感觉我们做的差不多,我是新手,想跟您请教一下,可能会耽误您一点时间,谢谢。

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Of Work with multiple database context
Layer of Abstraction for any DbContexts - With this method, you would essentially be creating a layer of abstraction between the UoW and ......
Read more >
c# - One transaction with multiple dbcontexts
In the first scenario, you are nesting DbContexts . A connection to the database is opened for each on of them. When you...
Read more >
C#/NET/EF Core: I have multiple DBContexts. How do I ...
I have multiple dbcontexts; one for each database connection. The databases are on separate servers. I need to do a bunch of CRUD...
Read more >
Implementing the infrastructure persistence layer with ...
The EF DbContext comes through the constructor through Dependency Injection. It is shared between multiple repositories within the same HTTP ...
Read more >
Repository Pattern in ASP.NET Core - Ultimate Guide
The other major advantage is that, multiple repository objects will have different instances of dbcontext within them. This can lead to data leaks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found