I am creating a simple website in DotNET Core, mainly to learn as I want to move away from PHP.
I wanted to run simple raw query to fill one table with some data. I also have few stored procedures that I would like to occasionally run.
I have found this learning material and tried to follow it (Updated in October this year): https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx
After getting errors I realised that latest version of EF does not contain definition for SqlQuery, and the closest you get is FromSQL.
I've found this issue discussion on github: https://github.com/aspnet/EntityFramework/issues/1862 And if you follow the conversation there you will see that support for raw SQL queries will be added in the next version coming out in Q4 / 2016 - Q1 / 2017
Question:
I seem to be getting a lot of partial information on how to work around this problem and I am getting some sort of semblance of what needs to be done. Still I cant get my head around the whole concept.
I have this simple DB:
AutoId Numbers
1 1
2 2
3 3
4 4
5 5
6 6
7 5
8 4
9 3
10 2
11 1
I've tried to access this data by writing this in my controller:
var graphs = _context.Graph.FromSql("SELECT * FROM dbo.Graph").ToArray();
Then I passed it in ViewBag:
ViewBag.GraphData = graphs;
But when I try to emit(is that the right word here?) the data to the view, I see this (I put it there in the view just to see what will happen really):
System.Collections.Generic.List`1[ASPNET_Core_1_0.Models.Graph]
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
ASPNET_Core_1_0.Models.Graph
My View file looks like this:
@model IEnumerable<ASPNET_Core_1_0.Models.Graph>
@{
ViewBag.Title = "Graphs";
}
@{
var data = ViewBag.GraphData;
}
<h2>Graphs</h2>
<div> @ViewBag.GraphData </div>
<br />
<div>
@foreach (var item in data) {
<br />
@item
}
</div>
====
Here is example from GIThub issue page:
E.g. assuming Product is an entity type and ProductListEntry is just an arbitrary CLR type that is not mapped in the mode, this works:
var data = db.Set<Product>()
.FromSql("SELECT * FROM Product WHERE 1=1")
.Select(t => new ProductListEntry{Id = t.Id, Name = t.Name})
.ToList();
But this doesn't:
var data = db.Set<ProductListEntry>()
.FromSql("SELECT Id, Name FROM Product WHERE 1=1")
.ToList();
Would anybody be kind enough to show me how to run this query and get actual results into my view instead of the object name? I understand that this might be dead easy question for some - for some reason I really struggle with understanding some of the basic concepts of DotNET Core and strongly typed language like C# hence questions like this.
Research:
Custom Data Access in EF for POCO
Raw SQL Query without DbSet - Entity Framework Core
Context does not contain a definition for ExecuteStoreCommand
Entity Framework 7 FromSql stored procedure return value
Entity Framework Core 1.1 Plans:
https://blogs.msdn.microsoft.com/dotnet/2016/07/29/entity-framework-core-1-1-plans/