2

I have a users table in my database that includes username, encrypted password, email and role id. I would like to pull back a list of all users excluding the password.

Through searching I have found answers for ef 6 and below but nothing that works in ef core.

This is what I have currently:

using (var context = new dbcontext())
{
     return context.Users.ToList()
}

I have tried

using (var context = new dbcontext())
{
     return context.Users
     .Select (u => new
     {
           Id = u.Id
           Username = u.Username
           Email = u.Email
      }
     .ToList()
}

Doing this returns nothing.

I have tried mapping a new entity and class to the table leaving out the password column but nothing gets returned. I think possibly because it's a required field.

Currently I am just converting the response to a new UserSummary class that doesn't have the password property but I'd think there is a way to do this without the password being returned at all.

jcalton88
  • 160
  • 1
  • 12
  • Do you make sure that table has rows ? Also you can exclude specific columns for your entity in ef core – H. Herzl Jul 03 '17 at 03:45
  • Yes I'm positive the table has does. With the first code given I get a response with all columns returned. If excluding columns is possible please answer with how. I have not been successful in this yet. – jcalton88 Jul 03 '17 at 03:52
  • What do you using ? data annotations or fluent api ? – H. Herzl Jul 03 '17 at 03:57
  • Fluent api. I'm not able to right now but in the morning I'll edit my question with how the entity is mapped – jcalton88 Jul 03 '17 at 04:10
  • Override the OnModelCreating method on your DbContext instance and add code like this: modelBuilder.Entity() .Ignore(p => p.Password); – H. Herzl Jul 03 '17 at 04:21
  • If you have data then 2nd query also works. Are you sure you are having data, you are looking at data in right place, you are actually calling ToList to fetch the data? – Smit Jul 14 '17 at 00:55
  • Possible duplicate of [Exclude a column from a select using LINQ](https://stackoverflow.com/questions/19463099/exclude-a-column-from-a-select-using-linq) – Michael Freidgeim Mar 29 '18 at 11:12

1 Answers1

1

You can create a class with the properties that you want to use ex:

origing class:

public class Rating
{
    public Rating() { }
    public int IdRating { get; private set; }
    public string IdUser { get; set; }
    public decimal Value { get; private set; }
    public string Comment { get; private set; }
    public bool IsEnabled { get; set; }
    public int IdCorrespondent { get; private set; }}

Destination class:

public class RatingView
{
    public Rating() { }
    public int IdRating { get; private set; }
    public decimal Value { get; private set; }
}

And map it on the repository with the method select ex:

public List<RatingView> ListRatings()
    {
        return _context.Ratings.Select(x => new RatingView
        {
            IdRating = x.IdRating ,
            Value = x.Value ,
        }).ToList();

    }

if you don't want to remodel the class u can make your own query using dapper, and u can find interesting things about dapper way here