Reputation: 53
I have got a login screen which gets UserName
and Password
, with the model Users
.
What I am trying to do is to find the user in my database, but I don't have any ID that can reference to which user I need.
How can I use SqlQuery() to select the user I want which will be alike to the user entered in the login page ?
Example: Let's say this is the DataBase
ID UserName Password Name Email Address RoleID
1 Locali 12563 Findme [email protected] asd 99 5
2 FindMe f452 asd [email protected] das 99 5
And the login Page User
object get:
UserName: FindMe
Password: f452
How can I search for something alike the login data and then retrieve all the missing data to the object ?
I have to do it with SQL
Upvotes: 1
Views: 36
Reputation: 2218
A sql-injection proof way:
using (var ctx = new DbContext())
{
var parameter1 = new SqlParameter("@UserName", "FindMe"));
var parameter1 = new SqlParameter("@Password", "f452"));
var user = ctx.Users.SqlQuery("Select * from Users where UserName = @UserName and Password = @Password", parameter1, parameter2)
.FirstOrDefault<Users>();
}
EF will retrieve all columns from the database and materialize them as object properties.
Upvotes: 3