Kelly
Kelly

Reputation: 187

Is it Possible to Force Properties Generated by Entity Framework to implement Interfaces?

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?

Upvotes: 1

Views: 431

Answers (3)

Kelly
Kelly

Reputation: 187

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.

Upvotes: 2

Jeroen Huinink
Jeroen Huinink

Reputation: 2017

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
{
}

Upvotes: 1

Frans Bouma
Frans Bouma

Reputation: 8357

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

Upvotes: 0

Related Questions