0

I'm using Unity.MVC for DI in my ASP.NET MVC 4.6 app. I have a service interface passed into the controller and that's working great. Now I want to pass in an interface to the EF context to the service but I'm not sure how to do this. I've read EF has this IObjectContextAdapter that I could pass into my service ctor and that works, but I need to then query the actual tables on inside my service from this context but because it's an IObjectContextAdapter it doesn't know my tables. How do I do this?

 public class ContactService : IContactService
    {
        //private ContactsEntities context;
        private IObjectContextAdapter context;

        // test ctor
        public ContactService(IObjectContextAdapter ctx)
        {
            context = ctx;
        }

        // prod ctor
        public ContactService()
        {
            context = new ContactsEntities();
        }

        List<Contact> GetAllContacts()
        {
            return (from c in context.ObjectContext.?? // I need to query the Contacts table that would be attached to the actual context I pass in but still keep the decoupling from using an Interface passed into the ctor

        }
    }

1 Answer 1

1

The IObjectContextAdapter is the type of ObjectContext property of DbContext.

You should subclass DbContext e.g. ContactsDatabaseContext

public class ContactsDatabaseContext : DbContext, IContactsDatabaseContext
{
  // ...
}

And then just register your ContactsDatabaseContext with your IoC container. Something like this:

container.RegisterType<IContactsDatabaseContext, ContactsDatabaseContext>();

Your ContactsDatabaseContext class and IContactsDatabaseContext interface should have properties of type DbSet<T> that refer to your tables e.g.:

IDbSet<BrandDb> Users { get; set; }

UPDATE:

Since you are using a generated file, then do this:

public partial class ContactsDatabaseContext : IContactsDatabaseContext
{
  // Expose the DbSets you want to use in your services
}
Sign up to request clarification or add additional context in comments.

13 Comments

I'm doing database first so touching the autogenerated ContactContext file is a no go right? Since it's autogenerated.
It's been a long time since I last used an EF generated file but that means you already that class. That's good. You may now register it. If you want you can still add the interface (if not already there by declaring the class with the partial keyword and assigning the interface. This way, you won't have to change the generated code in anyway (that's what partial is for).
Yeah, that helps. Kind of a bummer I have to maintain this manually vs EF doing it for me and creating an interface automatically and managing the DbSet's for me too. I can see forgetting to add DbSet's to the interface if I add tables to the database and update the model. With MS pushing DI so much you'd think they would do this for us. Thanks.
Good point. At least you have a way forward. Anyway, you'll find it's not too hard to remember as you'll have to add those properties to the interface when you want to use them... @user441521, if it works you should accept.
The time to accept wasn't up yet. I tried :). Good now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.