1

I want to pass Dbset Object to my method.

This is my Method:

public static class MyClass
    {
       public static Floor ReturnObject(this DbSet<Floor> floor, int Id)
        {
           var context = floor.passContext() as MyDBContext;
           var data = context.floors.Where(a =>a.Id == Id).FirstOrDefault();
           return ad;
        }
     }

   public class MyDBContext: DbContext
    {
          public DbSet<Floor> floors { get; set; }
    }

public static DbContext passContext<TEntity>(this DbSet<TEntity> dbSet)
            where TEntity : class
        {
                 //code....
        }

Now i want to use my method of static class MyClass that is ReturnObject.

I want to use ReturnObject method in my Controller.

I know i can declare it as private member like this in my controller:

This is my controller:

public class MyController
{
       private DbSet<Floor> floor;//but is this a good way???
         public ActionResult Index(int Id)
            {
              var data=MyClass.ReturnObject(????,Id)//what should come in place of question mark??
            }
}

How should i pass my First Parameter to ReturnObject Method???

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • 1
    You would have to pass in a DbSet to get the context, at which point you could have queried the dbset then, are you copying an example? You can either create a new context with using(var context = new passContext()) or use an IoC container like AutoFac, Unity, etc. which is best practice – PMC May 20 '15 at 07:25
  • @Maria Pithia why you want to pass it ? And what this method will do with this ? Do you want to filter data from this DbSet ? – Mairaj Ahmad May 20 '15 at 07:41
  • @MairajAhmad:i want to fetch data from my table floor by Id. – I Love Stackoverflow May 20 '15 at 07:43
  • You can fetch data directly here. But if you want to return data from this you don't need to pass it you can directly return data from that method. PLease add code of ReturnObject in your question. – Mairaj Ahmad May 20 '15 at 07:45
  • @PaulMcCowat:sir i have updated two field in my floor table with some value amd using this using(var context = new passContext()) only to fetch records isnt giving me the value of that newly updated field.the value of that two newly updated fields are always coming 0 when fetching records that is why i am dong like this – I Love Stackoverflow May 20 '15 at 08:44

1 Answers1

1

Change your ReturnObject method. It should only take Id as parameter and filter data than return object of Floor.

public static Floor ReturnObject(int Id)
{
  using(MyDBContext context = new  MyDBContext())
  {
       var data = context.floors.Where(a =>a.Id == Id).FirstOrDefault();
       return ad;
   }
}

And when you call it than only pass id as paramter

var data=MyClass.ReturnObject(Id);

This will return object of floor which will be stored in data.

Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40