views:

236

answers:

1

I have a stored procedure GetMyTime. It has one parameter int PersonID, and return a datatime. After add it to EDMX and import it as a function. Then I try to mock up 4.0 to write a code as below:

public ObjectResult<Nullable<global::System.DateTime>> GetMyTime(Nullable<global::System.Int32> PersonID)
        {
            ObjectParameter[] PersonIDParameters;
            if (PersonID.HasValue)
            {
                PersonIDParameters = new ObjectParameter[]{new ObjectParameter("PersonID", PersonID)};
            }
            else
            {
                PersonIDParameters = new ObjectParameter[]{new ObjectParameter("PersonID",typeof(global::System.Int32))};
            }
            return base.ExecuteFunction<Nullable<global::System.DateTime>>("GetMyTime", PersonIDParameters);  //this line cause error
        }

But I get a error at the last line: Error 2 The type 'System.DateTime?' cannot be used as type parameter 'TElement' in the generic type or method 'System.Data.Objects.ObjectContext.ExecuteFunction(string, params System.Data.Objects.ObjectParameter[])'. There is no boxing conversion from 'System.DateTime?' to 'System.Data.Objects.DataClasses.IEntityWithChangeTracker'.

How to fix it?

+1  A: 

According to MSDN, the "TElement" in ExecuteFunction<TElement> is an entity type. You're wanting to return a scalar value.

Returning scalar values is broken in v3.5, but you mention that you're working with EF v4, so it should work. My suggestion would be to wire up the scalar function import through the EDM designer, build, and use the generated code for guidance on exactly how to return a scalar value.

Dave Swersky
Thanks. I am working with 3.5, not 4.0(VS 2008 sp1 on XP). Is it possible to modify the code and make it working?
KentZhou
In 3.5 you can only map procs which return entity types.
Craig Stuntz