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.
The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database.
A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.
We can do that simply by using the “new” operator and selecting the properties from the object that we need. In this case, we only want to retrieve the Id and Title columns. There. That looks better.
DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table.
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();
"instead of lambda expression or query"
customerobject.Find(id);
More specifically:
var myDbSetTableEntity = context.MyDbSetTableEntity.Find(object key1, object key2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With