"..." cannot implement an interface member because it is not public

c# dbcontext entity-framework

Question

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?

1
10
2/14/2014 3:32:32 PM

Accepted Answer

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; }    
}
23
2/14/2014 3:40:43 PM

Popular Answer

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow