Base solution for your next web application
Open Closed

Directly Using a database context in app service error #1025


User avatar
0
liaquathussain created

when we try to use a DbContext directly to save some records it throws following exception on calling savechanges()

►System.Data.Entity.Core.EntityException {"The underlying provider failed on Open."} ►inner exception TransactionManagerCommunicationException {"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool."} ►Inner Exception COMException {"The transaction manager has disabled its support for remote/network transactions. (Exception from HRESULT: 0x8004D024)"}

I understand that normally we should avoid directly using dbContexts in AppService and use repositories but in my case I have to integrate my app with a third-party database application and for this I am using DB first approach to create that model.
please give me some direction how to handle this scenario. it's urgent.


5 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    When you use the second dbcontext in your app service, there will be 2 dbcontext and this requires MSDTC enabled. If you don't want it, just disable transaction for your app service method (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work#DocUowNoTransaction">http://www.aspnetboilerplate.com/Pages/ ... ransaction</a>).

    If you directly use DbContext in core or application layer, you will reference EF project and EF itself. I don't suggest it, but it's your choice.

    Do you have a seperated 2nd dbcontext? If so, can you share it for more help.

  • User Avatar
    0
    liaquathussain created

    yes i have a 2nd db context and as I have already mentioned that I am using a third party database so, I am unable to go with Code first approch. even if I choose to go with code first somehow(i know Its not suitable in this scenario). I would have to face same problem

    I would not be able to use Repositories as it requires IEntity Interface to be implemented and IEntity has an Id property to be used as primary key. which cannot be ensured that other database must be using a column with name Id as primary key.

    here is an example of entities from other dbContext

    public partial class Location { public string Code { get; set; } // primary key in this case public string Description { get; set; } public decimal StSpotAgmtNo { get; set; } ... }

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Yes, repository interface requires IEntity interface should be applied. You don't want to use ABP's infrastructure since your case does not fit. So, you can go as usual, just create a custom repository (do not inherit from irepository), example:

    public interface IMyRepositry
    {
        List<MyEntity> GetMyEntities(string someParameter);
    }
    

    then implement it in the EF project:

    public class MyRepository : IMyRepository, ITransientDependency
    {
       //implement your methods here...
    }
    

    In this MyRepository, you can use your 2nd dbcontext and query/change database. This approach does not use ABP infrastructure much. If you have many entities, you can create a simple base repository.

  • User Avatar
    0
    liaquathussain created

    if i choose to create my own repository implementation where would i register the Context Dependency and where should the context be kept in EntityFramework project or Some other project?

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    In this new repository, you can make just

    new YourDbContext()
    

    if you don't need dependency injection. And directly inherit YourDbContext from DbContext. If you want dependency injection work, implement ITransientDependency for YourDbContext and inject it into a repository or wherever you need it. Sure, put your dbcontext in EF project.