Datacontext submitchanges not updating
To-do this we'll open the Nerd Dinner database in the Server Explorer, and select the Tables we want to model in it: We can then drag the tables onto the LINQ to SQL designer surface.
When we do this LINQ to SQL will automatically create Dinner and RSVP classes using the schema of the tables (with class properties that map to the database table columns): By default the LINQ to SQL designer automatically "pluralizes" table and column names when it creates classes based on a database schema.
We can use this functionality to easily save the changes back to the database - without having to write any explicit SQL update code.
For example, the code below demonstrates how to use a LINQ query to retrieve a single Dinner object from the database, update two of the Dinner properties, and then save the changes back to the database: The Nerd Dinner Data Context object in the code above automatically tracked the property changes made to the Dinner object we retrieved from it.
When we called the "Submit Changes()" method, it will execute an appropriate SQL "UPDATE" statement to the database to persist the updated values back.
Below you can see an example of intellisense within Visual Studio when we create a new RSVP object and add it to a Dinner's RSVPs collection.
Notice how LINQ to SQL automatically added a "RSVPs" collection on the Dinner object: By adding the RSVP object to the Dinner's RSVPs collection we are telling LINQ to SQL to associate a foreign-key relationship between the Dinner and the RSVP row in our database: If you don't like how the designer has modeled or named a table association, you can override it.
LINQ to SQL will then takes care of generating the appropriate SQL execution logic to use at runtime when we interact and use them.