Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-static method requires a target. Entity Framework 5 Code First

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?

like image 840
ilivewithian Avatar asked Nov 03 '12 15:11

ilivewithian


People also ask

What are non-static methods?

A non-static method belongs to an object of the class and you have to create an instance of the class to access it. Non-static methods can access any static method and any static variable without creating an instance of the class.

Can we call non-static method from static class in C#?

We can call non-static method from static method by creating instance of class belongs to method, eg) main() method is also static method and we can call non-static method from main() method . Even private methods can be called from static methods with class instance. yes.

What is a non-static field?

Non-static fields are instance fields of an object. They can only be accessed or invoked through an object reference. The value of static variable remains constant throughout the class. The value of Non-static variables changes as the objects has their own copy of these variables.

Can't be referenced from a static context?

And if no class instance is created, the non-static variable is never initialized and there is no value to reference. For the same reasons, a non-static method cannot be referenced from a static context, either, as the compiler cannot tell which particular object the non-static member belongs to.


1 Answers

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.

like image 186
ilivewithian Avatar answered Sep 24 '22 05:09

ilivewithian