53

I am getting the error "Non-static method requires a target." when I run the following query:

var allPartners = DbContext.User
                           .Include(u => u.Businesses)
                           .Where(u => u.Businesses.Any(x => x.Id == currentBusinessId))
                           .ToList();

My entites are defines like this:

public class User : Entity
{
    public virtual List<Business> Businesses { get; set; }
}

public class Business : Entity
{
    public virtual List<User> Users { get; set; }
}

public class Entity
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
}

And my context is configured like this;

public class Context : DbContext, IDatabaseSession
{
    public DbSet<Business> Business { get; set; }
    public DbSet<User> User { get; set; }

    public Context()
        : base("DefaultConnection")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Conventions.Remove
            <System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();

        Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());

        modelBuilder.Entity<User>()
            .HasMany(u => u.Businesses)
            .WithMany(b => b.Users);
    }
}

What have I done wrong?

2
  • Can you show the type of exception, possible inner exceptions and call stack?
    – Slauma
    Nov 3, 2012 at 20:50
  • 1
    It does not seem that the problem is in the code you showed.
    – Pawel
    Nov 8, 2012 at 4:00

1 Answer 1

125

The problem boiled down to the query. My original question had this query:

var allPartners = DbContext.User
                       .Include(u => u.Businesses)
                       .Where(u => u.Businesses.Any(x => x.Id == currentBusinessId))
                       .ToList();

Which wasn't quite accurate, I had in fact removed the error in an attempt to ask my question succinctly. The query was actually:

var currentBusiness = GetBusiness();
var allPartners = DbContext.User
                       .Include(u => u.Businesses)
                       .Where(u => u.Businesses.Any(x => x.Id == currentBusiness.Id))
                       .ToList();

When the GetBusiness method returned null the error was thrown. Simply ensuring that I don't pass a null object into the expression made the error stop.

3
  • 1
    thank you, that's exactly what is happening to me. I would have chased my tail for hours on this.
    – Greg Veres
    Apr 10, 2017 at 17:51
  • Same here - random :) BUT very helpful answer - thank you!
    – Jimmy T.
    May 14, 2021 at 13:23
  • I got the same problem. It helped me. Thanks
    – JS Guru
    Jul 25, 2021 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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