Title: An object specified for refresh is not recognized
Tags: c#;asp.net-mvc;linq-to-sql
Question: I have a update function like this:

```public void Update(HomeBanner homebanner)
    {
        homebanner.EnsureValid();
        DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner);
        DataSource.DataContext.SubmitChanges();
    }
```

And i write a update controller

```[AcceptVerbs(HttpVerbs.Post)]
    //[ValidateAntiForgeryToken]
    [ValidateInput(false)]
    public ActionResult ManageImages(int ? id,FormCollection form)
    {
        HomeBanner homebanner= BannerRepository.RetrieveById(id);
        this.TryUpdateModel(homebanner);
        string photoName = saveImage("photo");
        if (photoName != string.Empty)
        homebanner.ImageID = photoName;
        BannerRepository.Update(homebanner);
        return RedirectToAction("list", "Admin");

    }
```

and then the view:

```<% using (Html.BeginForm("ManageImages", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
   {%&gt;
<h3&gt;Manage Images</h3&gt;
         <div class="label-field"&gt;
        <label for="ID"&gt;Chọn vị trí:</label&gt;
         <%= Html.DropDownList("ID", DataHelper.Banner().ToList().ToSelectList("value", "name",Model.HomeBanner.ID.ToString()))%&gt;
         </div&gt;
        <div class="label-field"&gt;
        <label for="photo"&gt;
            Chọn hình</label&gt;
        <input type="file" name="photo" value=""/&gt;
        </div&gt;
        <div class="label-field"&gt;
        <label for="Link"&gt;
            Liên kết</label&gt;
        <input type="text" name="Link"/&gt;
        </div&gt;
        <p&gt;
            <input type="submit" value="Lưu" /&gt;
        </p&gt;
<% } %&gt;
```

It get data as well but update step is not success: It mark right here

```DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner);
```

and throw exception: An object specified for refresh is not recognized.
I dont know why, i see data filled to object when i debug. Plz someone help me!



Here is another answer: I bump into this error because i was trying to update a just created object that did not exist yet on the database.


Here is another answer: Check the instance of DataContext there, maybe you are using different instance in which original object doesn't exists.

If it doesn't exist, you must first attach object to data context, then call refresh.

P.S. a tip: Make model or service for interacting with data, in controller it looks messy ;)'

Comment for this answer: Yes, it's really..Thank you. Can u believe, finally i find out the problem: i forgot create primary key for HomeBanner table!!!!

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:01/19/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc><linq-to-sql>', 'Title': 'An object specified for refresh is not recognized', 'ViewCount': 3051, 'id': 8923039, 'user_AboutMe': '<p>working with asp.net-mvc framework, jquery,..</p>\n', 'user_AccountId': 1043638.0, 'user_DisplayName': 'Sunshine jp', 'user_Reputation': 85.0, 'user_Views': 16.0, 'user_id': 1049083.0}}