简体   繁体   中英

Can't Get ALl Records with Stored Procedure in EF

I use Entity Framework 5 . In my project I created a stored procedure for getting all records and added it to Entity Framework.

I want get all records, but the stored procedures returns an int .

When I want get all records with this stored procedure, and iterate over them using foreach

C# code :

using (var context=new AngularJsEntities())
{
            foreach (comment item in context.SelectAllComments())
            {
               // do something
            }
}

How can I do this?

I search at google but I can't find useful result.

My stored procedure code:

create procedure SelectComments
AS
Begin
   select CommentBody, Name, Email, Id 
   from comments
end

After solving:

在此处输入图片说明

Most likely, you used Database.ExecuteSqlCommand rather than Database.SqlQuery<T> . The former returns int and is only useful for SQL queries that don't return data (update, delete, etc.) while the latter returns DbRawSqlQuery<T> . For example, your SelectAllComments method should do something like the following

var comments = context.Database.SqlQuery<Comment>("exec SelectComments").ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM