[英]How can I find a object from dbcontext with string parameter, not with Find(primary key) function
是的,context.Accounts.Find(id)其中id是主键,它确实为我带来了正确的帐户。 但我想从context.Accounts搜索字符串用户名,而不是主键。
更准确地说,方法:
public void Authenticate(string username, password)
{ result = false;
Accounts test = new Accounts();
test.User = username;
Accounts dbEntry = new Accounts();
dbEntry = context.Accounts_.Find(test.User);
if (dbEntry.Password == password)
if(dbEntry.Admin == 1)
result = true;
return result;
}
所以用户名不再是主键,如何在没有Find()函数的情况下找到合适的用户名?
更换
Accounts test = new Accounts();
test.User = username;
Accounts dbEntry = new Accounts();
dbEntry = context.Accounts_.Find(test.User);
同
var dbEntry = context.Accounts_.FirstOrDefault(acc => acc.username == username);
或者在您的Accounts_中调用'Username'的任何内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.