1

Noob question

I'm using Entity Framework in my project, it generates the entities in the designer, like:

public partial class CLMEntities : ObjectContext

I have another class in the project that I want CLMEntities to inherit from for more functionality:

public class MoreFunctionality

...of course I can't have that inheritance because it already inherits from ObjectContext

Any ideas how I can do this?

2
  • 1
    Don't you want to switch to POCOs generation? It supported by both EF 4.x and 5.x (and also both VS2010 and 2012). Then your entity class will inherit no other classes. Commented Feb 3, 2013 at 9:00
  • +1 yep get on the new generation templates. MUCH easier. The basic tips below from Steve Wilkes still apply... You are just using DBContext instead of ObjectContext. Not sure what design Problem requires a "context" to inherit from custom code. You can extend partial classes of course. Including adding different constructors. Commented Feb 3, 2013 at 13:17

1 Answer 1

3

Couple of options, depending on what the functionality in MoreFunctionality is:

1: Make it a generic decorator class instead of a parent class, like this:

public class MoreFunctionality<TEntities> where TEntities : ObjectContext
{
    private readonly TEntities _objectContext;

    public MoreFunctionality(TEntities objectContext)
    {
        this._objectContext = objectContext;
    }

    public TEntities ObjectContext
    {
        get { return this._objectContext; }
    }

    // Other behaviour
}

...and pass the decorator around instead of the ObjectContext.

2: Make it an interface and add the behaviour using extension methods, like this:

public interface IMoreFunctionality
{
}

public partial class CLMEntities : ObjectContext, IMoreFunctionality
{
}

public static class MoreFunctionalityExtensions
{
    public static void SomeFunctionality(
        this IMoreFunctionality moreFunctionality)
    {
        // Do something...
    }

    public static void SomeMoreFunctionality(
        this IMoreFunctionality moreFunctionality)
    {
        // Do something else...
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

TNX , in the option 1 if i genrate the edmx again by the genrator it will Changed ? PLZ you can explian more specific the option 2 thanks alot!
Option 1 won't be affected by CLMEntities being regenerated. Give me an example of the behaviour you have in MoreFunctionality and I'll see if I can update my answer using it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.