locked
what does it means Set<T>().Find(id); in EF ? RRS feed

  • Question

  • User828542847 posted

    just reading a code snippet on generic controller usage but stuck some where to understand the code. see the code please

    public abstract class GenericController<T> : Controller
        where T : class
    {
        protected YourEFContext _dataSource;
    
        public GenericController()
        {
            _dataSource = new YourEFContext();
        }
    
        public virtual ActionResult Details(int id)
        {
            var model = _dataSource.Set<T>().Find(id);
            return View(model);
        }
    }
    
    public class CustomerController : GenericController<Customer>
    {
    
    }

    this line is not clear to me what is happening var model = _dataSource.Set<T>().Find(id);

    what does it means Set<T>().Find(id); ?

    please help me to understand this code.

    Wednesday, July 26, 2017 8:49 AM

Answers

  • User-1508012637 posted

    Hi tapan2017,

    what does it means Set<T>().Find(id);

    The Details method you give above is the same with the following code from the function aspect:<o:p></o:p>

    public virtual ActionResult Details(int id)
    {
        var model = _dataSource.Customer.Find(id);//_dataSource.Customer returns DbSet<Customer>, which is the same with _dataSource.Set<T>() when T is Customer
        return View(model);
    }
    

    The scenario you might use Set<T> method is that you haven’t know all the entity types while you’re designing the DbContext. In that case you don’t have to wait the DbContext designed finished and you could design your controller first or at the same time.

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 2:19 AM

All replies

  • User1387190246 posted

    Set<T> will return the Entities Collection, so you can find the record with primary key using Find()

    Wednesday, July 26, 2017 3:32 PM
  • User-1508012637 posted

    Hi tapan2017,

    what does it means Set<T>().Find(id);

    The Details method you give above is the same with the following code from the function aspect:<o:p></o:p>

    public virtual ActionResult Details(int id)
    {
        var model = _dataSource.Customer.Find(id);//_dataSource.Customer returns DbSet<Customer>, which is the same with _dataSource.Set<T>() when T is Customer
        return View(model);
    }
    

    The scenario you might use Set<T> method is that you haven’t know all the entity types while you’re designing the DbContext. In that case you don’t have to wait the DbContext designed finished and you could design your controller first or at the same time.

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Velen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 27, 2017 2:19 AM