How to update with Entity Framework in mvc3?

I Have a table in my databse that one of the columns named NumOfView shows the number of clicks on a link. That link shows some info from a row in this table. I used onclick event in anchor tag like this:

onclick=@Url.Action("NumOfClicks", "AdvertiseHelper",new { id =[i] })

In NumOfClicks function I used this code

 public void NumOfClicks (int id)
        Ad ad1 = new Ad();
        var advert = (from ad in storedb.Ads where ad.AdId == id select     ad.NumOfView).First();
        advert += 1;



is the amount of


in table that I want to increase it 1 unit. but I don't know how to continue coding for update this value in table. Can anybody help me please?


This is the basics of how it should be done. First you have to select the record itself from the database rather than the number of clicks. Increment the number of clicks on that record, then submit the changes to the DataContext.

public void IncrementClickCount(int id)
    var advert = 
        (from ad in storedb.Ads 
         where ad.AdId == id
         select ad).Single();   // Select the row in the database represented by "ad"
    advert.NumOfView++;         // Perform the increment on the column

    // SubmitChanges is how an update is done in Linq2Sql 
    // This requires a .dbml file for the database object file
    // storedb.SubmitChanges();    // Assumes storedb is a valid DataContext, updates

    // SaveChanges is how an update is done in EntityFramework
    // It is recognizable because it uses an .edmx file for its database object

This was similarly asked in SO question: Entity Framework - Update a row in a table

Need Your Help

how to call a method of multiple arguments with delay

ios methods selector

I'm trying to call a method after some delay.

How to make blocks of unit tests in R?

r unit-testing

I have started to make unit-tests for my functions in R, using svUnit (docs). I have done the test for the functions in a file, then for the ones in another file, and I have created a mainTest, whe...