繁体   English   中英

如何从dbcontext中使用字符串参数查找对象,而不是使用查找(主键)函数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM