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

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

Question

I want to locate an object that corresponds to a certain primary key and I have a table called "Customer" and its ORMapping Entity Customer in the entity framework. something similarcustomerobject.getbjectByID() instead of a query or lambda expression.

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 is supposing that the CustomerID column in your customer database serves as the primary key.

Using the Find method of DbSet:

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

A lambda expression is used:

 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