1

I want to be able to do the following:

I have a model and inside there I do have an entity.

This entity has the following structure:

public class Client
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}   

What I want now, is to just get the client name based on the id. Therefore I wrote a stored procedure which is doing this.

CREATE PROCEDURE [Client].[GetBasics]
@Id INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


SELECT 
    Name
FROM Client.Client
INNER JOIN Client.Validity ON ClientId = Client.Id
WHERE
    Client.Id = @Id; 
 END

Now, going back to VS, I do update the model from the database with the stored procedure included.

Next step is to map this stored procedure to the client entity as a function import.

This also works fine.

Trying now to load one client's name results into an error during runtime...

"The data reader is incompatible with the specified 'CSTestModel.Client'. A member of the type, 'Id', does not have a corresponding column in the data reader with the same name."

I am OK with the message. I know how to fix this (returning as result set Id, Name, Description).

My idea behind this question is the following:

I just want to load parts of the entity, not the complete entity itself. I have a restriction here to just use stored procedures for the entire communication towards/from the database.

Is there a solution to my problem (except creating complex types, LINQ on the result set itself)? And if yes, can someone point me to the right direction?

Many thanks,

Dimi

1 Answer 1

1

Just project onto a POCO:

var q = from c in Context.Clients
        select new NameOnlyPresentation
                   {
                       Id = c.Id,
                       Name = c.Name
                   };

... or just the name:

public string ClientName(int id)
{
    return (from c in Context.Clients
            where c.Id == id
            select c.Name).FirstOrDefault();
}
Sign up to request clarification or add additional context in comments.

4 Comments

well, forgot to mention that I know about this one too :) Will edit my question to be more precise there.
If you know about the really simple solution, what is there about your problem which is driving you to ask for a very difficult solution? Sounds like there's something missing here.
The use of stored procedures and getting the benefits of the mapping feature of EF is driving me here
I see partially populating an object as a useful thing, also. Having to generate a distinct poco for each type of partial population is cumbersome, and seems unnecessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.