views:

200

answers:

3

Example Interface:

public Interface IEntity
     Property ID() as Integer
end Interface

I want all my EF objects to implement this interface on there Primary Keys.

Is this possible?

A: 

The classes are partial, so it should be very easy to do.

Frans Bouma
+1  A: 

Yes, you can. The classes that the designer generates are declared partial. In a seperate source file you can declare additional methods for these classes. You can also declare specific interfaces that are already implemented by the generated class.

/* This is the interface that you want to have implemented. */
public interface ISomething
{
    void DoSomething();
}

/* This would be part of the generated class */
partial class PartialClass
{
    public void DoSomething()
    {
    }
}

/* This would be your own extension */
partial class PartialClass : ISomething
{
}
Jeroen Huinink
+2  A: 

This seems very easy to do in CSharp but in VB you have to specifically declare which Properties/Functions/Subs are Implementing the Interface:

public Property Id() as Integer Implements IEntity.Id

Unfortunately I had to Rip out the designer file and modify the generated properties. I ended up getting rid of the Generated File all together and now keep my Models in separate classes with all of the Attribute mappings.

Kelly
Note I switched to Linq over the EF Framework due to lack of documentation for Attribute Mapping
Kelly