16

I have a class that is generated by a third party tool:

public partial class CloudDataContext : DbContext 
{
    // ...SNIPPED... 
    public DbSet<User> Users { get; set; } 

}

I create a partial class and then assign an interface so that I can inject this class later:

public partial class CloudDataContext : IDataContext
{

}   

The IDataContext has the single property Users.

This won't compile, the compiler complains that the interface isn't implemented.

If I move the interface to the generated class, it works fine. I can't do that though as it's generated code.

How can I apply an interface to a partial class to expose the class as defined above?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Ian Vink
  • 66,960
  • 104
  • 341
  • 555
  • Does DBContext not implement IDataContext? – Preet Sangha Dec 11 '13 at 00:34
  • Can you show the definition of `IDataContext` and the implementation of `CloudDataContext`? I just ran [this example](http://dotnetfiddle.net/ExiSIp) and it works just fine. – Jeroen Vannevel Dec 11 '13 at 00:37
  • I'm going to go out on a pretty far limb here: are the `using` directives at the top of the two class files different? Is it possible that in the one code file, `IDataContext` resolves to _one_ interface, and in the other code file `IDataContext` resolves to a _second different_ interface? – Chris Sinclair Dec 11 '13 at 01:01

5 Answers5

31

The problem must be somewhere else, because you can implement interface in the other part of partial class than it's set on. I just tried following and it compiles just fine:

public interface IFoo
{
    int Bar { get; set; }
}

public partial class Foo
{
    public int Bar { get; set; }
}

public partial class Foo : IFoo
{

}

The properties probably use different types in interface and class.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
  • 7
    Turns out it was a typo in the name space of one of the partials. Once matched, it worked. – Ian Vink Dec 11 '13 at 18:58
  • 3
    In my case it was because the new partial class file I generated had a different class name. Remember to change the class name to match the one in the other file... – Andrew Sep 09 '18 at 00:56
4

Here's a quick checklist. Do the classes have identical:

  • Names?
  • Namespaces?
  • Access modifiers?

Example:

  • You decide to split an existing class into two files.
  • The original file's namespace doesn't match its folder path.
  • Consequently, the new class file you create has a mismatching namespace.
  • Build fails.
Eric Eskildsen
  • 4,269
  • 2
  • 38
  • 55
1

IN my Case problem was that interface Method that was implemented in other part of the partial class was not compiling and C# was giving error of not implemented Method

0

In my case, the interface method signature didn't mention a value (uint direction) that the actual method expected. This showed up as the interface having errors in one of the partial classes. Make sure that the interface for a method is actually the same as the method signature itself. D'oh.

Per
  • 175
  • 1
  • 7
0

Make sure that all files of the partial class have "C# compiler" Build Action in VS.

Greg Z.
  • 1,376
  • 14
  • 17