How can i return a dataReader using Entity Framework 6.1?

.net asp.net c# entity-framework entity-framework-6

Question

As stated in the question. The majority of my code is written in entity framework, but occasionally I need to execute and return data from a SQL table that is outside the context of entity framework.

1
3
7/10/2015 8:31:31 AM

Popular Answer

When utilizing Entity Framework, you could, for instance:

using (var context = new BloggingContext()) 
{ 
    var blogNames = context.Database.SqlQuery<string>( 
                       "SELECT Name FROM dbo.Blogs").ToList(); 
}

You can create your own class and use that if you want to return a more complicated type. It will function as long as the characteristics correspond to the names of the columns you choose. Let's create a class then:

public class MyClass
{
    public int Id { get; set; }
    public string UserName { get; set; }
}

Add to that:

List<MyClass> result = ctx
    .Database.SqlQuery<MyClass>("SELECT Id, UserName FROM dbo.Users")
    .ToList();
5
7/10/2015 9:23:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow