Reputation: 497
I have a question regarding Linq and updating a record in the database. I don't know how to update a record when the fields that you use are not known. It is hard for me to explain this so I use the following example:
Customer c = (from x in dataBase.Customers
where x.Name == "Test"
selext x).First();
c.Name = "New Name";
dataBase.SaveChanges();
In this example you'll update the name field in the table Customers in the database. Lets say that Customer has an ID, a name and a phone number. In the example you'll only update name in the database. And if I want to update the phone number I'll just add c.Phonenumber = 01234; and it will also update the phone number.
Now, the user uses text boxes to fill in new data, however, it is not required to change all data. So you can update name and phone number, or either one of them. The problem that I have is that I don't know how to determine what the user filled in and how to determine what fields to select and update. If I have a customers table with 10 different fields it is possible that a user wants to change 8 fields and I don't know how to tell the program that it has to update those 8 fields. I hope you can understand my explanation and can help me with this. Thanks in advance.
Upvotes: 0
Views: 152
Reputation: 11277
Your ObjectContext
will keep track of changes for you. It does this because all your entities (e.g. Customer
) will inherit EntityObject
and INotifyPropertyChanged
Calling SaveChanges
will persist all the changes to the database in a transaction.
This mean that you don't need to keep track of changes - EF does that for you.
Upvotes: 2