Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

Filtering DbSet in EntityFramework CodeFirst

This is the ApplicationUser:

public class ApplicationUser : IdentityUser<long>
{
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public UserTypes Type { get; set; }
    public string FullName { get { return $"{Firstname ?? ""} {Lastname ?? ""}".Trim(); } }
}

We have 3 different UserTypes (Provider, Supporter, NormalUser (which is the ApplicationUser))

public class Provider : ApplicationUser{
   // Provider related virtual Icollections
}

public class Supporter : ApplicationUser{
   // Supporter related virtual Icollections
}

Now in the ApplicationDbContext I want to have these DbSets beside ApplicationUser:

public virtual DbSet<Provider> Providers{get;set;}
public virtual DbSet<Supporter> Supporters{get;set;}

Which DbSet<Provider> should return the ApplicationUsers that their UserTypes equals 2 (for example)

Upvotes: 0

Views: 646

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34150

     public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<long>, long>
        {

            public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
                : base(options)
            {
            }
            public ApplicationDbContext()
                : base()
            {
            }

            public virtual IEnumerable<Provider> Providers
            {
                get
                {
                    return (IEnumerable<Provider>)Users.Where(z => z.Type == UserTypes.Provider).AsEnumerable();
                }
            }
         }

Upvotes: 1

Related Questions