asp.net-core


ASP.NET Core and EF Core 1.1 - Diplay Data using Stored Procedure


I have problem with my query. I want to display the result on a view.
[HttpGet]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index()
{
return View(await _Context.Employee
.FromSql("EXEC sp_GetLoanDetails")
.ToArrayAsync());
}
Here is my list of items I want to view:
public class StoredProcRow
{
[Key]
public int empID { get; set; }
public string empFullName { get; set; }
public double EducationalLoan { get; set; }
public double PettyCash { get; set; }
public double BusinessLoan { get; set; }
public double ApplianceLoan { get; set; }
public double EmergencyLoan { get; set; }
public double AllPurposeLoan { get; set; }
public double KAPUSOIILoan { get; set; }
public double FiestaLoan { get; set; }
public double SalaryLoan { get; set; }
public double Pledge { get; set; }
public double PagIbigLoan { get; set; }
public double SSSLoan { get; set; }
public double AllAroundLoan { get; set; }
public double Total { get; set; }
}
Note: These entities name are the same as entities on column name in sp_GetLoanDetails
Is this achievable right now on EF Core 1.1? Or do I need to go back to manual ADO.NET code?
Thanks!
While support for stored procedures isn’t completely there with Entity Framework Core yet, you still can use FromSql to consume stored procedures with it.
In order to do that, the database context needs to know the entity you are mapping to from the stored procedure. Unfortunately, the only way to do that right now is to actually define it as an entity in the database context:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<StoredProcRow>(entity =>
{
// …
});
}
Then, you can consume the stored procedure by running the FromSql method on a set for that entity:
public virtual IQueryable<StoredProcRow> GetLoanDetails()
{
return Set<StoredProcRow>().FromSql("[sp_GetLoanDetails]").AsNoTracking();
}
Note that I’m using a AsNoTracking here to avoid the data context to track changes to entities that come from the stored procedure (since you don’t have a way to update them anyway). Also I’m using Set<T>() inside the method to avoid having to expose the type as a member on the database context since you cannot use the set without the stored procedure anyway.
Btw. you don’t need (not sure if that even works) EXEC in the sql statement you pass to FromSql. Just pass the stored procedure name and any arguments to it, e.g.:
Set<MyEntity>().FromSql("[SomeStoredProcedure]");
Set<MyEntity>().FromSql("[SProcWithOneArgument] #Arg = {0}");
Set<MyEntity>().FromSql("[SProcWithTwoArguments] #Arg1 = {0}, Arg2 = {1}");

Related Links

asp.net core mvc6 seeding Roles produces different results when repeating database creation
Setting the version number for .NET Core projects
Can one use Reportviewer Control in ASP.net Core
Migrating from OWIN to ASP.NET Core
Understanding ASP.NET Core Dependencies and Capabilities
dnx-watch stopped working and throwing ArgumentException because directory was not found
how to render date part segments with anchor taghelper in ASP.NET Core
Unable to target netstandard or netstandardapp in VS2015
Error building AspNet.Security.OpenIdConnect.Server
How do I declare a development dependency in project.json?
Class Library Package for Onion Architecture - version issues
ASP.NET requirements for ClaimTypes
ASP.NET Core ignores ASPNET_ENV and Hosting:Environment
No authentication handler is configured to handle the scheme Microsoft.AspNet.Identity.Application
How to Highlight navbar Menu Items in using TagHelpers (Asp.Net.MVC6 6.0.0-rc1.0-final)
How to get #Url.Action value inside a controller

Categories

HOME
ibm-bluemix
jdo
openmp
magnific-popup
tizen
dictionary
vmware
react-router
bookshelf.js
getelementsbytagname
jgroups
add
gitpitch
cloudkit
nano-server
spring-kafka
midi
spring-xd
izpack
vb.net-2010
gz
designer
karma-jasmine
kryo
core-text
zurb-foundation-6
hammerspoon
c++-amp
google-cloud-nl
tasklet
code-contracts
avcapturesession
repo
uiswipegesturerecognizer
ghost4j
picasso
qwerty
hot-module-replacement
code-search-engine
bootstrapper
android-tabhost
hp-ux
sfdc
google-closure
fakeiteasy
arena-simulation
node-sass
wso2carbon
multipeer-connectivity
jquery-nestable
pnotify
sybase-asa
iso8601
spring-cache
smart-table
android-textview
mathematica-frontend
holder.js
lua-5.1
dlna
hexdump
bluegiga
two-factor-authentication
uid
unity-networking
spim
intellij-14
qdialog
ideamart
processmodel
fscommand
dia
onactivityresult
project-planning
dml
funcunit
expected-exception
doskey
seaside
jboss-weld
resty-gwt
gnu-smalltalk
inbox
batterylevel
html4
seed
drools-planner
e4x
cisco-jtapi
gwt-ext
gcj
genshi
self-extracting
lang
cxxtest
sudzc
suppress
preference
boost-smart-ptr
simpletest
pascal-fc
nerddinner

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App