0

I will state up front I am still very new to asp.net core and entity framework but I am familiar with ADO and recordset which it seems entity framework is built from. I am struggling to use entity framework because I am able to run the query but I am not sure how to use the results and most help docs I see online only discuss the procedures and methods and not how to use the results. I am building login functionality on my site and have developed the following code to query DB in my UserAccount table. FOr this login I really only want the Username, Password, and the ID however I have multiple fields in this table that I dont need for this aspect. I come from using ADO and recordsets so really I just prefer to use raw sql and determine what i want to do with those results and not bind them to some objects which it seems entity framework does for the most part. I do liek the ease of use of the entity framework and using parameters though so I prefer to just go this route (not sure if this is bad practice or not). Everything works great except when I add ToList it starts to say error "InvalidOperationException: The required column 'AccessLevel' was not present in the results of a 'FromSql' operation." I am not even tryign to use that field in the query so not sure why that is even coming up and I am using a rawsql query method so why is it trying to see what I have in the model for that table? Lastly, this shoudl return a single record in which case I want to pull the password value from that record field similar to how i use to do in ADO as you will see in my passwordhash verification but I cannot figure out how to even pull that from the query result. Thanks for any help on this as I am getting a headache trying to learn this stuff!!

 var UserResults = _context.UserAccounts.FromSqlInterpolated($"SELECT USERACCOUNT.USERNAME, USERACCOUNT.PASSWORD, USERACCOUNT.ID,USERACCOUNT.ACCESS_LEVEL FROM DBO.USERACCOUNT WHERE USERACCOUNT.USERNAME={UserName}"); if (UserResults.Count() == 1) //if we have more than 1 result we have security issue so do not allow login { var passwordHasher = new PasswordHasher<string>(); var hashedPassword = passwordHasher.HashPassword(null, Password); var testResults = UserResults.ToList(); if (passwordHasher.VerifyHashedPassword(null, hashedPassword, THIS SHOULD BE VALUE FROM DB RIGHT HERE!!!) == PasswordVerificationResult.Success) { var claims = new List<Claim> { new Claim(ClaimTypes.Name, UserName) }; 

My DBContext is as follows:

public partial class LoginDBContext : DbContext { public DbSet<UserAccount> UserAccounts { get; set; } public LoginDBContext(DbContextOptions<LoginDBContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<UserAccount>(entity => { //entity.ToTable("UserAccount"); entity.HasNoKey(); //entity.Property(e => e.Id) // .HasMaxLength(50) // .IsUnicode(false); //entity.Property(e => e.Username) // .HasMaxLength(50) // .IsUnicode(false); //entity.Property(e => e.Password) // .HasMaxLength(50) // .IsUnicode(false); }); } } 

    1 Answer 1

    1

    If you don't want to load all columns of the user table you can return anonymous object with properties you need or create a class for the columns you need and return it.

    var users = _context.UserAccounts .Where(a => a.UserName == UserName) .Select(a => new { a.Id, a.UserName, a.Password }) .ToArray(); if (users.Length == 1) { var user = users.First(); if (passwordHasher.VerifyHashedPassword(null, hashedPassword, user.Password) == PasswordVerificationResult.Success) { // ... your magic } } 
    7
    • So you are not even using the fromsqlinterpolated()? Is there a way to do this with the raw sql such as my code?
      – Qiuzman
      CommentedJan 8, 2021 at 20:50
    • FromRowSql expect to return an entity so it requires that all columns are provided. You said I am struggling to use entity framework - you do because you not using it as what it was designed for. Usage of FromRawSql is provided only for "exceptional" cases. If you are not going to use statically typed queries, then your struggle will continue - look for some other ORM frameworks ;)
      – Fabio
      CommentedJan 8, 2021 at 21:01
    • You are absolutely right I should probably force myself to keep using it to learn it. I will adapt my code to wha tyou put but to understand further why UserAccount would even work do I need to adjust my DBContext file at all from what I have above. I commented out ToTable("UserAccount") but I am assuming I should use that to create the link so it knows the table? Also, do you typically create a DBContext in the same file for every table (Ie, modelBuilder.Entity<TableName> for each table in one file).
      – Qiuzman
      CommentedJan 8, 2021 at 21:16
    • EF will use entity type name as a table name by default, use ToTable('name") when table and type names are different. You can configure multiple tables in one DbContext in one file.
      – Fabio
      CommentedJan 8, 2021 at 21:25
    • Ahh gotcha. I guess ModelBuilder is really isnt necessary for me then in this case since its all identical. Just curious of you opinion on this. If I am very familiar with SQL and have used ADO quite often in VBA projects, would you think I would be better suited using ADO.NET for an application to communicate with a database or do you think entity framework is probably much better to force to learn from scratch?
      – Qiuzman
      CommentedJan 8, 2021 at 21:52

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.