为什么 ExecuteFunction 方法只能通过 ObjectContext 子类中的 base.ExecuteFunction 来使用?

发布于 2024-09-05 10:20:23 字数 1928 浏览 13 评论 0原文

我正在尝试从站点存储库中的 objectcontext 对象调用 ObjectContext.ExecuteFunction。

该存储库是通用的,因此我拥有的只是一个 ObjectContext 对象,而不是实际代表实体框架中的特定对象的对象。

以下是使用 ExecuteFunction 方法生成的代码示例:

[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public global::System.Data.Objects.ObjectResult<ArtistSearchVariation> FindSearchVariation(string source)
{
    global::System.Data.Objects.ObjectParameter sourceParameter;
    if ((source != null))
    {
        sourceParameter = new global::System.Data.Objects.ObjectParameter("Source", source);
    }
    else
    {
        sourceParameter = new global::System.Data.Objects.ObjectParameter("Source", typeof(string));
    }
    return base.ExecuteFunction<ArtistSearchVariation>("FindSearchVariation", sourceParameter);   
}

但我想做的是这样的...

public class Repository<E, C> : IRepository<E, C>, IDisposable
    where E : EntityObject
    where C : ObjectContext
{
    private readonly C _ctx;

    // ...

    public ObjectResult<E> ExecuteFunction(string functionName, params[])
    {
        // Create object parameters

        return _ctx.ExecuteFunction<E>(functionName, /* parameters */)
    }
 }

任何人都知道为什么我必须从 base 调用 ExecuteFunction code> 而不是 _ctx

另外,有什么办法可以像我写的那样做吗?我真的很想保持我的存储库的通用性,但是由于必须执行存储过程,它看起来越来越困难...

更新:这是我尝试过的方法,该方法不会出现在智能感知中/它给了我当我尝试使用它进行编译时出现错误

public ArtistSearchVariation findSearchVariation(string source)
{
    System.Data.Objects.ObjextContext _ctx = new ObjectContext(/* connection string */);
    System.Data.Objects.ObjectParameter sourceParam = new  ObjectParameter("Source", source);

    return _ctx.ExecuteFunction<ArtistSearchVariation>("FindSearchVariation", sourceParam);   
}

谢谢,
马特

I'm trying to call ObjectContext.ExecuteFunction from my objectcontext object in the repository of my site.

The repository is generic, so all I have is an ObjectContext object, rather than one that actually represents my specific one from the Entity Framework.

Here's an example of code that was generated that uses the ExecuteFunction method:

[global::System.CodeDom.Compiler.GeneratedCode("System.Data.Entity.Design.EntityClassGenerator", "4.0.0.0")]
public global::System.Data.Objects.ObjectResult<ArtistSearchVariation> FindSearchVariation(string source)
{
    global::System.Data.Objects.ObjectParameter sourceParameter;
    if ((source != null))
    {
        sourceParameter = new global::System.Data.Objects.ObjectParameter("Source", source);
    }
    else
    {
        sourceParameter = new global::System.Data.Objects.ObjectParameter("Source", typeof(string));
    }
    return base.ExecuteFunction<ArtistSearchVariation>("FindSearchVariation", sourceParameter);   
}

But what I would like to do is something like this...

public class Repository<E, C> : IRepository<E, C>, IDisposable
    where E : EntityObject
    where C : ObjectContext
{
    private readonly C _ctx;

    // ...

    public ObjectResult<E> ExecuteFunction(string functionName, params[])
    {
        // Create object parameters

        return _ctx.ExecuteFunction<E>(functionName, /* parameters */)
    }
 }

Anyone know why I have to call ExecuteFunction from base instead of _ctx?

Also, is there any way to do something like I've written out? I would really like to keep my repository generic, but with having to execute stored procedures it's looking more and more difficult...

Update: Here's what I've tried and the method does not show up in intellisense/it gives me an error when I try to compile with it

public ArtistSearchVariation findSearchVariation(string source)
{
    System.Data.Objects.ObjextContext _ctx = new ObjectContext(/* connection string */);
    System.Data.Objects.ObjectParameter sourceParam = new  ObjectParameter("Source", source);

    return _ctx.ExecuteFunction<ArtistSearchVariation>("FindSearchVariation", sourceParam);   
}

Thanks,
Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-09-12 10:20:23

您不必使用 base.ExecuteFunction,ExecuteFunction 方法(和重载)是公共的,不受保护,因此您可以从外部站点调用它们。你打电话有困难吗?

You don't have to use base.ExecuteFunction, the ExecuteFunction method (and overloads) are public, not protected, so you can call them from external sites. Are you having trouble calling it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文