0

I have the following schema :

public class Provider
    {

        [Key]
        public int ProviderId { get; set; }
        [ForeignKey("ProviderId")]
        public ApplicationUser User;

        public ICollection<ServiceProvider> Services { get; set; }
    }

public class ApplicationUser : IdentityUser
{
    public ApplicationUser() : base() { }

    public string Name { get; set; }
    public string Address { get; set; }
    public string Photo { get; set; }
    public string BusinessName { get; set; }
}

In my backend, I have an email as input and I'm trying to check if there is any provider with that email. I tried to use the following code :

if (context.Provider.Any(o => o.User.Email == input_mail) == false)

but I got a null pointer exception..

I know that I can use linku syntax :

    var q = from au in _context.ApplicationUser
            join p in _context.Provider on au.Id equals p.ProviderId
            where au.Email=input_mail;

Any way to do it using the models context ? instead of linku

JeyJ
  • 3,582
  • 4
  • 35
  • 83

2 Answers2

0

If I understood your question then following sentence is sufficient no need to add FALSE if (context.Provider.Any(o => o.User.Email == input_mail))

Chand Jogani
  • 146
  • 12
0

override Email and test it for nullorempty in the model.

// Override auto-implemented property with ordinary property
   // to provide specialized accessor behavior.
    public override string Email
    {
        get
        {
            return email;//declare this private string also in model
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                name = value;
            }
            else
            {
                email= "Unknown";
            }
        }
    } 
terrencep
  • 675
  • 5
  • 16
  • you're saying input_mail is not null, I am saying o.User.Email is null. Another option is to use a foreach instead of any(). Or also use Where and check if the returned list is count 0 – terrencep Oct 10 '19 at 17:43
  • The problem is that the User is null, it doesnt matter if I try to use o.User.Email or o.User.Name. It fails to fetch the User because it needs to join an IdentityUser table – JeyJ Oct 10 '19 at 17:44
  • try o.User?.Email – terrencep Oct 10 '19 at 17:49