1

I am doing a simple edit in ASP.NET MVC. The action result code is below. This example here says I can do the following.

Code example:

public ActionResult Edit(int id)
   {
       using (var db = new BlogDataEntities())
       {
           return View(db.Blogs.Find(id));
       }
    }

But when I try to do it, I cannot get Find property for db.Amodel.? What am I missing?

My code:

public ActionResult Edit(string id)
    {
        using (var db = new dbAEntities())
        {
           return View(db.Amodel.Find(id));//This is not working. Find is not     
                                           //recognzied and intellisense does 
                                           //not give me that option too.
        }           
    }
7
  • Looks like Amodel doesn't have a Find() method. Commented Feb 24, 2012 at 19:24
  • Isnt the find method automatically created .. the example in the link I provided, does not talk about writing own Find method.. using the method in the link, all the methods necessary like Add, Find should be created automatically .. isnt it? Commented Feb 24, 2012 at 19:26
  • What type is Amodel? Does it have similar characteristics to Blogs, i.e. was it created using Database First? Commented Feb 24, 2012 at 19:27
  • EntityObject ... Yes.I am following the sample, even though my database fields and name are different I am also using Database first.. lot of the code in the background is automatically generated.. when I created the models automatically by picking ADO.NET Entity Data Model option... Commented Feb 24, 2012 at 19:29
  • Create functionality worked well.. without any issues.. I followed the same sample .. Commented Feb 24, 2012 at 19:31

1 Answer 1

2

They are referring to the Entity Framework 4.1 which seems to introduce the Find method. You are probably using an older version. Rewrite the code to:

public ActionResult Edit(string id)     
{         
   using (var db = new dbAEntities()) {
        return View(db.Amodel.FirstOrDefault(item => item.id == id));
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I was guessing something like this was happening.. thanks for pointing out. This worked.
SingleOrDefault is probably more appropriate. Since Find is looking at the key properties, there should only be a single entity with the same key(s).
@ZVenue That example is with entity framework 4.1 if you want to use 4.1 download it via package manager msdn.microsoft.com/en-us/data/ee712906
Also I am in the same boat with this line of code in the sample.... db.Entry(blog).State = System.Data.EntityState.Modified; cant seem to make it work in my copy..

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.