SQL query in EntityFramework

1

I want to do manual sql queries but using the .net entity framework.

ex:

select from database where id == 20 ;

and no:

Item algo = new Item();
algo.name =
algo.lastName =

algo.Add();

understand me ????

    
asked by Daniel Batiz 20.06.2018 в 20:58
source

1 answer

1

Your question is understandable, although it is poorly written, first we go for examples:

SQL queries for entities

using (var context = new BloggingContext()) 
{ 
    var blogs = context.Blogs.SqlQuery("SELECT * FROM dbo.Blogs").ToList(); 
}

Loading entities from stored procedures

using (var context = new BloggingContext()) 
{ 
    var blogs = context.Blogs.SqlQuery("dbo.GetBlogs").ToList(); 
}

You can also send parameters:

using (var context = new BloggingContext()) 
{ 
    var blogId = 1; 

    var blogs = context.Blogs.SqlQuery("dbo.GetBlogById @p0", blogId).Single(); 
}

Writing SQL queries for non-entity types

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

Sending raw commands to the database

It beats me that this is what you are looking for:

using (var context = new BloggingContext()) 
{ 
    context.Database.ExecuteSqlCommand( 
        "UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1"); 
}

Reference: link

    
answered by 20.06.2018 / 21:06
source