简体   繁体   English

使用存储过程从 AspNetUsers 表中检索所有或单个用户

[英]Using a stored procedure to retrieve all or single user from AspNetUsers table

I see a lot of information on ASP.Net Core Identity storing DATA with the stored procedure and I tried and use the same method to retrieve / Get all or a single user detail from AspNetUsers table in SQL, but did not succeed.我看到很多关于 ASP.Net Core Identity 使用存储过程存储 DATA 的信息,我尝试并使用相同的方法从 SQL 中的 AspNetUsers 表中检索/获取所有或单个用户详细信息,但没有成功。 below is my code.下面是我的代码。

My Stored Procedure我的存储过程

alter PROCEDURE [dbo].[spEmployeeDetails]


@EmployeeID varchar(50) = ''

AS
BEGIN
    IF(@EmployeeID !='')
    BEGIN
        SELECT * FROM AspNetUsers WHERE EmployeeId like @EmployeeID + '%'
    END
    ELSE
    BEGIN
        SELECT * FROM AspNetUsers
    END
END

Model class Model class


 public class AppUsers:IdentityUser
    {
      
        public string EmployeeId { get; set; }  
        public string FName { get; set; }  
        public string LName { get; set; }  
        public string Nationality { get; set; }  
        public string PositionTitle { get; set; }  
        public string Department { get; set; }
        public string DepartmentCode { get; set; }
        public string PosCode { get; set; }
        public string Grade { get; set; }
}

service class服务 class


  public AppUsers GetaUser(string id)
        {
          
            var getuser = context.AppUsers.FromSqlRaw($"spEmployeeDetails {id}").ToList();

            return getuser.FirstOrDefault();

        }

the error I am getting is我得到的错误是

InvalidOperationException: 'FromSqlRaw' or 'FromSqlInterpolated' was called with non-composable SQL and with a query composing over it. Consider calling 'AsEnumerable' after the method to perform the composition on the client side.

I tried this code also but no luck我也试过这段代码,但没有运气

     IEnumerable<AppraisalUsers> objd = context.AppraisalUsers.FromSqlRaw($"spEmployeeDetails {id}").AsEnumerable<AppraisalUsers>();

can anyone help me to retrieve a user from AspNetUsers table generated by Identity framework谁能帮我从身份框架生成的 AspNetUsers 表中检索用户

I am working on Core3.1我正在研究Core3.1

Instead of FromSqlRaw you should use FromSqlInterPolated to stop sql injection attacks.您应该使用FromSqlInterPolated而不是FromSqlRaw来阻止 sql 注入攻击。

You can try adding the SQL Parameter name like so您可以尝试像这样添加 SQL 参数名称

context.AppUsers.FromSqlInterPolated ($"exec spEmployeeDetails @EmployeeID={id}")

You could also write this as pure ef core code as您也可以将其编写为纯 ef 核心代码

return context.AppUsers.FirstOrDefault(e => e.EmployeeId == id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用实体框架从存储过程中检索表数据 - Retrieve table data from stored procedure using entity framework 如何从存储过程中检索单个值 - How to Retrieve a Single Value from Stored Procedure 如何从存储过程检索表到数据表? - How can I retrieve a table from stored procedure to a datatable? 使用单个用户帐户时如何在AspNetUsers表中设置PhoneNumber - How to set PhoneNumber in AspNetUsers table when using individual user accounts 如何从WebAPI更新AspNetUsers表中现有用户记录的密码 - How to update password for the existing user record in AspNetUsers table from WebAPI 无法使用存储过程从 SQL Server 检索数据 - Cannot retrieve data from SQL Server using Stored Procedure 使用ObjectContext从存储过程中检索多个结果集 - Retrieve multiple result sets from a stored procedure using ObjectContext 使用Linq-to-SQL从存储过程中检索多个数据 - Retrieve multiple data from stored procedure using Linq-to-SQL 如何使用实体框架检索从存储过程返回的值? - How to retrieve a value returned from a stored procedure using entity Framework? 使用LINQ从存储过程获取单列结果作为列表 - Get single column results as a list from a stored procedure using LINQ
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM