Mapping stored procedure results to a custom class using EF Core FromSql returns instances with default values

时光总嘲笑我的痴心妄想 提交于 2021-01-29 19:00:38

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!