扩展实体框架模型以包括新类

时间:2013-02-03 07:49:19

标签: .net entity-framework entity-framework-4 edmx

Noob问题

我在我的项目中使用Entity Framework,它在设计器中生成实体,如:

public partial class CLMEntities : ObjectContext

我在项目中有另一个类,我希望CLMEntities继承以获得更多功能:

public class MoreFunctionality

...当然我不能继承这个继承,因为它已经从ObjectContext继承了

我有什么想法可以做到这一点?

1 个答案:

答案 0 :(得分:3)

一些选项,取决于MoreFunctionality中的功能:

1:将其设为通用decorator类而不是父类,如下所示:

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
}

...然后绕过装饰器而不是ObjectContext

2:将其设为界面并使用extension methods添加行为,如下所示:

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