0

I am trying to find an user that has a custom property "Token" that I added with database migration, I tried to use:

var adb = new AppDbContext();
var found = adb.Users.Where(u => u.Token == "ABDJ_SJ_ETC");//Error
//OR found = adb.Users.SingleOrDefault(); Error too

My AppDbContext class

AppDbContext.cs

namespace MyApp{
    public class AppDbContext : IdentityDbContext<AppUser>{
         public AppDbContext(): base("DefaultConnection"){}
    }
}

AppUser.cs

namespace MyApp{
    public class AppUser : IdentityUser{
        public string Token {get;set;}
    }
}

The error is "System.Data.Entity.IDbSet does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Data.Entity.IDbSet could be found (are you missing a using directive or an assembly reference?)

Without the Where line my app runs correctly, add Users to db with my Token, etc. What i need to do to find an AppUser by that custom field??

3
  • Add using System.Linq;
    – p.s.w.g
    Mar 4, 2015 at 19:16
  • I followed this tutorial without problems: link
    – RPAZ
    Mar 4, 2015 at 19:20
  • @p.s.w.g Thanks, Visual Studio didn't show me what using directive I needed to use, I added it and that solved the problem
    – RPAZ
    Mar 4, 2015 at 19:27

3 Answers 3

1

In Visual Studio, you can rollover the error line and it will suggest the appropriate missing using.

Remember to included in your code the .SingleOrDefault() statement in line:

var found = adb.Users.Where(u => u.Token == "ABDJ_SJ_ETC").SingleOrDefault()
1
  • Update:It's posible to use alt+enter and Visual Studio will suggest to add the using or include inline the respective namespace in the first and second options respectively. Sep 19, 2017 at 19:48
0

The problem was that Visual Studio didn't suggest me any dependencies to resolve, and i didn't know which directive needed to use,

I added

using System.Linq;

and that solved the problem

Thanks @p.s.w.g for helping me

0

use bottom code for get custom ApplicationUser in identity with custom property

var context=new AuthContext();
var result=context.Users.OfType<ApplicationUser>.SingleOrDefault(item => item.NationalCode == "");

NationalCode is custom property in ApplicationUser ApplicationUser is custom user that implement IdentityUser

public class ApplicationUser :  IdentityUser
{
    public string NationalCode { get; set; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.