问题
I have a stored procedure like this:
CREATE PROCEDURE PLogin
@pin varchar(50), @ipaddress varchar(50)
AS
BEGIN
SET NOCOUNT ON;
//Some code here ...
SELECT * FROM [dbo].[QRelaceUzivatele_Aktivni] WHERE Pin = @pin AND IPAdresa=@ipaddress
END
I need to execute it from ASP MVC using EF Core 2.1.
I have used the approach mentioned here.
I have a custom C# class AktivniRelaceUzivatele
that maps the fields of the QRelaceUzivatele_Aktivni
view.
public class AktivniRelaceUzivatele
{
int ProvozID { get; set; }
string NazevProvozu { get; set; }
int UzivatelID { get; set; }
string PIN { get; set; }
string Nick { get; set; }
In MVC controller I call:
var qpin = new SqlParameter("p", pin);
var qip = new SqlParameter("ip", ip);
var ar = db.Query<AktivniRelaceUzivatele>()
.FromSql("EXECUTE dbo.PLogin @pin=@p, @ipaddress=@ip", qpin, qip).FirstOrDefault();
In dbContext class I have added:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Query<AktivniRelaceUzivatele>();
I get no compile error the stored procedure gets executed but the result values are not insterted into objects's properties, instead I get instances that contain only default values like null
, 0 etc.
回答1:
Can u try to add public classifiers to class variables? I think I got the similar issues, where I missed both getters and setters and public classifiers.
来源:https://stackoverflow.com/questions/52703599/mapping-stored-procedure-results-to-a-custom-class-using-ef-core-fromsql-returns