使用ASP.NET身份并使用这样的实体框架将UserStore定制为我自己的数据库表集时
public Task<MyUser> FindByNameAsync(string userName)
{
var context = new CompanyDbContext();
Task<MyUser> task = context.MyUsers.Where(u => u.UserName == userName)
.FirstOrDefaultAsync();
return task;
}
并像这样调用它可以正常工作:
MyUser user = await userManager.FindAsync(context.UserName, context.Password);
现在,我想用如下的硬编码用户列表代替它:
public Task<MyUser> FindByNameAsync(string userName)
{
var myusers = new List<MyUser>
{
new MyUser() { Id="1", UserName = "tom", Password = "secret" },
new MyUser() { Id="2", UserName = "mary", Password = "supersecret" }
};
Task<MyUser> task = new Task<MyUser>(() => myusers.SingleOrDefault(
u => u.UserName == userName));
return task;
}
但这行不通,因为它挂在这条线上
MyUser user = await userManager.FindAsync(context.UserName, context.Password);
任何建议,将不胜感激。
最佳答案
尝试这个...
public Task<MyUser> FindByNameAsync(string userName)
{
var myusers = new List<MyUser>
{
new MyUser() { Id="1", UserName = "tom", Password = "secret" },
new MyUser() { Id="2", UserName = "mary", Password = "supersecret" }
};
var task = Task.FromResult(myusers.SingleOrDefault(u => u.UserName == userName));
return task;
}
关于c# - 如何替换 Entity Framework FirstOrDefaultAsync,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21560758/