1

I'm using Entity Framework. I need to find a record using its primary key.

I try like this.

tabCom com = db.tabCom.Find(3);

but there is error and it says

'System.Data.Objects.ObjectSet' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Data.Objects.ObjectSet' could be found (are you missing a using directive or an assembly reference?)

How to find the data using primary key. what the wrong with my code or Is there is another way?

1
  • MSDN link Commented Aug 26, 2013 at 2:20

2 Answers 2

6
tabCom com = db.tabCom.Single(tab => tab.PrimaryKeyColumn == 3);
Sign up to request clarification or add additional context in comments.

Comments

1

The Find method was introduced in EF 4.1.

In EF 5 it has optimizations that will check the in-memory cache before going off to the database. Using Single(entity => entity.PrimaryKey == id) will not provide the same optimization.

Please check which version of EF your code is referencing.

2 Comments

As best I can tell, Find is a member of DbSet. From OP: error ... says 'System.Data.Objects.**ObjectSet*' does not contain a definition for 'Find'*
@ta.speot.is that means not only do you have to worry about which version of EF, but also if it is DB-first or Code-first, with each getting slightly different features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.