1

Yesterday, when working with some Entity Framework stuff, I tried to write this, and got the error "ObjectContext isn't a member" etc.

context.ObjectContext.Refresh(RefreshMode.StoreWins, myObject);

Now, context inherits the DbContext object, documented here: http://msdn.microsoft.com/en-us/library/system.data.entity.dbcontext(v=vs.103).aspx

This is normal inheritance of the "is a" type - my context object IS a DbContext object. The DbContext class implements the IObjectContextAdapter interface, so I would expect that the code above should work - the member function declared by the interface is required to be implemented in the DbContext class, or I would expect it to fail compilation with the "you must implement the interface" error. However, when we looked in the generated class definition, it wasn't there!

Eventually, I implemented the call like this, which the compiler accepted.

((IObjectContextAdapter)context).ObjectContext.Refresh(RefreshMode.StoreWins, myObject);

Can anyone explain why I had to do that, when the following code is the same situation and works fine?

interface IMyInterface {
    int MyInterfaceMemberFunction();
}

public class MyObjectClass : IMyInterface {
    public int MyInterfaceMemberFunction() { return 17; }
}

public class MyTestClass {
    public int MyTestFunction() {
        MyObjectClass m = new MyObjectClass();
        return m.MyInterfaceMemberFunction(); //error here for EF context
    }
}
Jasmine
  • 4,003
  • 2
  • 29
  • 39
  • possible duplicate of [C# Interfaces. Implicit implementation versus Explicit implementation](http://stackoverflow.com/questions/143405/c-sharp-interfaces-implicit-implementation-versus-explicit-implementation) – CSharpie May 21 '14 at 18:41
  • No it's not the same question. This is a problem with the Entity Framework. My code is just an example of what I would expect to happen. – Jasmine May 21 '14 at 18:44
  • Oh you're right... it's a case of that. Not a duplicate question though, this is a question about what's happening in the entity framework, not 'in general' with explicit implementation. – Jasmine May 21 '14 at 18:46
  • Yeah couldn't really decide. Hope it helps though. – CSharpie May 21 '14 at 18:47
  • Yeah definitely helpful, we got our answer, thanks! – Jasmine May 21 '14 at 18:48

1 Answers1

1

This is because the ObjectContext property is explicitly implemented. To use your example to demonstrate it:

interface IMyInterface {
    int MyInterfaceMemberFunction();
}

public class MyObjectClass : IMyInterface {
    // this line changed:
    int IMyInterface.MyInterfaceMemberFunction() { return 17; }
}

public class MyTestClass {
    public int MyTestFunction() {
        MyObjectClass m = new MyObjectClass();
        //return m.MyInterfaceMemberFunction(); //error here
        return ((IMyInterface)m).MyInterfaceMemberFunction(); //this works
    }
}

Explicit implementation is necessary if you have two interfaces that define different members with the same name. It can also be used to hide certain interface members, if you wish them to not be noticed by people using your class normally. For example, List<T> implements IList, but explicitly implements IList.Add(object) so that you don't accidentally try to add anything but a T to a List<T>.

Tim S.
  • 55,448
  • 7
  • 96
  • 122