views:

234

answers:

1

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

+1  A: 

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?

Matthew Abbott
Yes, the function's not showing up. I put an example of what I've tried in my question.
Matt