0

I can't figure out what I am doing wrong I am new to this entity framework. I am just trying to call this simple stored procedure which returns one or none persons.

This is the error I get:

Cannot implicitly convert type 'System.Data.Objects.ObjectResult' to 'Model.Person'

Stored Procedure:

Create PROCEDURE [dbo].[GetPersonByID] 
@personID int

AS
BEGIN
    SELECT        PersonID, FirstName, LastName, DOB, Notes,  Reason, Diagnosis, DateEntered
    FROM            Person
    WHERE         PersonID = @personID
END

I updated my entity and see the stored procedure here. I also right clicked on my stored procedure and added a function import and tied it to the Person Entity.

Code:

private static Person GetPersonByID(int personID)
{
    try
    {
        ModelEntities context = new ModelEntities();

        return context.GetPersonByID(personID);
    }
    catch (Exception ex)
    {
        return null;
    }
}

2 Answers 2

1

The function GetPersonByID is returning an ObjectResult type, you need to convert it to your model. Something like this:

var persons = context.GetPersonByID(personID).Select(p => new Model.Person
{
    Name = p.Name,
    Age p.Age,
    ... etc ...
});

return persons.SingleOrDefault();
Sign up to request clarification or add additional context in comments.

3 Comments

Shouldn't it recognize that it is a person object through the Function Imports? What happens if you have the same code but return a dataset of all the persons? It seems like there is a more straight forward solution.
Think of the ObjectResult return type as a collection of your objects. Your code is trying to assign that collection to an individual Model.Person.
If the return type is actually ObjectResult<Person> then you may be able to simplify it to return context.GetPersonByID(personID).SingleOrDefault()
0

Seems there is mismatch between the SP result set and yours entity. Try to update entity same as result set or vice versa.

** It would be greatly helpful, if you can provide entity code as well. That way we can test within my test environment.

1 Comment

I wish I could upload not not sure how or what to do. I updated my entity but I still have the same error.

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.