【发布时间】:2020-09-06 05:02:46
【问题描述】:
我使用的是 EF Core 3.1,并且我有这个存储过程调用:
public async Task<string> UserSessionGet(string user, string password, string ip)
{
IList<SessionItem> lst = null;
try
{
// Processing.
string sqlQuery = "EXEC [dbo].[GetUserSession] @UserLogin, @UserPassword, @IP, @ErrorCode OUTPUT, @ErrorText OUTPUT";
int errorCode = 0;
string errorText = string.Empty;
lst = await this.Set<SessionItem>().FromSqlRaw(sqlQuery,
new SqlParameter("@UserLogin", user ?? (object)DBNull.Value),
new SqlParameter("@UserPassword", password ?? (object)DBNull.Value),
new SqlParameter("@IP", ip ?? (object)DBNull.Value),
new SqlParameter("@ErrorCode", errorCode) { Direction = ParameterDirection.Output},
new SqlParameter("@ErrorText", errorText) { Direction = ParameterDirection.Output }
).ToListAsync();
}
catch (Exception ex)
{
throw ex;
}
// Info.
return lst.FirstOrDefault()?.Session;
}
实体:
public class SessionItem
{
[NotMapped]
public string Session { get; set; }
}
设置:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SessionItem>().HasNoKey();
}
当我尝试执行这段代码时,抛出异常:
序列不包含任何元素
当我在登录尝试失败时删除 NotMapped 注释时,我收到异常
“FromSql”操作的结果中不存在所需的“会话”列
当用户输入错误的密码或用户名时,只返回@ErrorCode和@ErrorText,但没有任何数据。
https://i.ibb.co/pf4KzHz/Failure-Login.png
调用成功时只返回一列,名为Session。
https://i.ibb.co/tL3CR1H/2020-09-06-07-57-12.png
我该怎么办?
我有另一个具有不同列集和相同行为的存储过程。
【问题讨论】:
-
您需要与预期结果匹配,请显示实际列名,此处不可见:i.ibb.co/tL3CR1H/2020-09-06-07-57-12.png
-
@ErikEJ 列名是 Session
-
您需要更改存储过程以至少返回一行,即使在登录错误的情况下也是如此
标签: c# stored-procedures entity-framework-core ef-core-3.1