15
public interface IDatabaseContext : IDisposable {

    IDbSet<MyEntity1> Entities1 { get; set; }

}

public class MyDbContext : DbContext, IDatabaseContext {

    IDbSet<MyEntity1> Entities1 { get; set; }

}

Can't compile because of the error described in here: http://msdn.microsoft.com/en-Us/library/bb384253(v=vs.90).aspx

However, this makes no sence since the interface obviously IS public. What could be the error here?

Acrotygma
  • 2,531
  • 3
  • 27
  • 54
  • 3
    It makes perfect sense, when you interpret "it" as referring to the subject of the sentence: the intended implementation member, not the interface member. (In other words, MyDbContext.Entities1... which is private, currently.) – Jon Skeet Feb 14 '14 at 15:34

5 Answers5

32

However, this makes no sence since the interface obviously IS public. What could be the error here?

No, it isn't. Members on classes are private by default. This Entities1 is private:

public class MyDbContext : DbContext, IDatabaseContext {    
    IDbSet<MyEntity1> Entities1 { get; set; }    
}

Note that this is different to interfaces, where everything is public and access modifiers do not make sense. So: either make the member public:

public class MyDbContext : DbContext, IDatabaseContext {    
    public IDbSet<MyEntity1> Entities1 { get; set; }    
}

or do an explicit interface implementation:

public class MyDbContext : DbContext, IDatabaseContext {    
    IDbSet<MyEntity1> IDatabaseContext.Entities1 { get; set; }    
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 5
    Dear god. How did I not see that? Well, seems like I eternalized myself forever in the SO hall of shame. :/ – Acrotygma Feb 14 '14 at 15:46
  • 13
    @Acrotygma rest assured: when somebody else spots something, it is only through hard-won battle experience of having made the same mistake themselves, probably several times – Marc Gravell Feb 14 '14 at 15:47
  • The reason to use `explicit interface implementation` is to control visibility for some methods otherwise `public` access modifier is used. [Here's](https://www.devcurry.com/2015/03/c-cannot-implement-interface-member.html) a good article about it too. – stanimirsp Jan 19 '23 at 08:19
8

When implementing an interface member in the class, it should be public

See: Interfaces (C# Programming Guide)

To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.

public class MyDbContext : DbContext, IDatabaseContext {

    public IDbSet<MyEntity1> Entities1 { get; set; }
}

Or as @Marc Gravell said in comment you can do Explicit interface implemenation, More could be found at this answer

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
2

I had the same issue only to discover that I forgot to make the method of the interface public. Make sure all the methods of the interface are public too.

befree2j
  • 361
  • 5
  • 11
0

To simplify the answer/logic -

Your problem is that interfaces ARE MEANT to be public in the inheriting class (this is why it is called an interface).

Therefore you must mark the implementation in the derived class as public (in MyDbContext).

Lior
  • 284
  • 1
  • 6
-1

Interface member can be implemeted as Protected too. If you dont want we can leave it as abstract incase of Abstract classes.

  • 1
    No, they can't; a `protected` member does not implicitly implement an interface, and an *explicit* interface implementation is not `protected`. – Marc Gravell Feb 14 '14 at 15:42