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

Accessing DBConetxt from Application Layer #1703


User avatar
0
maharatha created

how do I access the DB Context from application layer ?

I am trying to run code similar to below in application Layer :

using (var ctx = new TestContext()) { ctx.AuditEntries.Where(item); ctx.AuditEntries.Where<Entity_Basic>(item.ID); ctx.AuditEntries.Where<Entity_Basic>(101); }

Can you point me to the right direction ?


5 Answer(s)
  • User Avatar
    0
    maharatha created

    To be more speicifc I am trying to use :

    <a class="postlink" href="https://github.com/zzzprojects/EntityFramework-Plus/wiki/EF-Audit-%7C-Entity-Framework-Audit-Trail-Context-and-Track-Changes">https://github.com/zzzprojects/EntityFr ... ck-Changes</a>

    I am now trying to access the Audit entry table using the code like this :

    using (var ctx = new CORPACCOUNTINGDbContext()) {

                var auditlog = Z.EntityFramework.Plus.AuditExtensions.Where&lt;MyTable&gt;(ctx.AuditEntries, Id).ToList();
    
            }
    

    It works fine if I put in a custom repository but I would like to create a repository which can pass "MyTable" as a parameter to the custom repository and use it wherever i like.

    Else I need a way where I can access the DB context from application layer.

    I tried injecting the IDbcontextprovider but probably I am not doing the right way, could you please let me know how to access the DB context from application layer.

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    This should work:

    public class YourAppService : YourAppServiceBase, IYourAppService
    {
        private readonly IDbContextProvider<YourDbContext> _dbContextProvider;
        
        public YourAppService(IDbContextProvider<YourDbContext> dbContextProvider)
        {
            _dbContextProvider = dbContextProvider;
        }
        
        public void MyMethod()
        {
            var people = _dbContextProvider.GetDbContext().Persons.ToList();
        }
    }
    
  • User Avatar
    0
    maharatha created

    That's the problem i am struggling with

    private readonly IDbContextProvider<YourDbContext> _dbContextProvider;

    I am unable to access my the DB context while declaring. It doesn't show up in intellisense as well as when i copy paste too it's doesn't recongize.

    Am I missing something here .

  • User Avatar
    0
    hikalkan created
    Support Team

    Your .Application project probably does not have a reference to the .EntityFramework project. Just add a reference for it.

  • User Avatar
    0
    maharatha created

    Thank You Hilkan.

    That worked, but I had put a Application reference in the Entityframework few days back. So it was throwing a circular reference error.

    I removed the Application reference from Entityframework and added the Entityframework reference in application and it worked.

    Thank you so much.