In entity framework, find an object for a given primary key.

.net c# entity-framework system.data.sqlite

Question

I have a table "Customer" and it's corresponding ORMapping Entity Customer in entity framework and I want to find an object corresponding to a given primary key. Something like customerobject.getbjectByID() instead of lambda expression or query.

1
7
12/7/2015 6:17:05 AM

Popular Answer

var Customer = from c in datacontext.Customer
               where c.CustomerID == your_key
               select c;

That's assuming your customer table has a CustomerID column and that is the primary key.

Using DbSet's Find method:

Customer customer= db.Customer.Find(your_key);

Using a lambda expression:

 var customer= dataContext.Customer.Where(x=>x.CustomerID==your_key).FirstOrDefault();
9
12/4/2011 3:18:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow