how to retrieve a sql row using Entity and .edmx file (newbie question) RRS feed

  • Question

  • please bear with me, I was used to used Sql. EF is a few quantum jumps for me.

    I spent hours of searching but I am not getting it

    in my edmx I have an entity Customers from the sql server table customer whose key if customerID

    how do I get the entity instance from the customer table where customerID="SS0456"

    when I tried

    using (Customers context = new Customers)



    I got using Flagged as Customers type used in a using statement must be implicity convertible to System IDisposable. how do get IDisposable?

    on the other hand I also tried to figure out how to use Single


    Saturday, March 3, 2012 8:33 AM


  • Hi there,

    You have to query the "Context" for your customer. Because you triedto instanciate the Customers Collection directly you got the "IDosposible" error.

    Here some examples who you could get your customer entity:

            Customer LoadCustomer(int ID)
                Customer cs = null;
                using (CustomerEntities ctx = new CustomerEntities () )
                    // One possible way
                    IQueryable<Customer> qry1 = ctx.Customer
                                                   .Where(c => c.CustomerID == ID);
                    Customer Customer1 = qry1.Single();
                    // a little bit more compact ...
                    var Customer1a = ctx.Customer.Where(c => c.CustomerID == ID).Single();
                    // another way ...
                    IQueryable<Customer> qry2 = from c in ctx.Customer
                                                where c.CustomerID == ID
                                                select c;
                    Customer Customer2 = qry2.Single();
                    cs = Customer2;
                return cs;

    Hope that helps,


    • Proposed as answer by Allen_MSDN Tuesday, March 6, 2012 2:21 AM
    • Marked as answer by Allen_MSDN Thursday, March 8, 2012 2:06 AM
    Saturday, March 3, 2012 11:37 AM