Updating a record in linq




















Therefore, if you take a book that's in one category and add it to a different category, you are effectively moving it to that new category even though you didn't explicitly ask for this. When we remove a book from a category, our OnBookRemoved delegate clears the Category property from that Book instance. We, for some reason, have two copies of Programming Ruby , so let's remove the Programming Ruby 1.

Finally, we can complete our M:M relationships. These are a little trickier because we want to keep our Join table BookAuthor out of our public interface so callers can work directly with Book s and Author s and not have to care how they're joined together.

If you're coding along, then you already added logic into BookAuthor so that if either side updates a BookAuthor relationship, it handles synchronizing both the Book and the Author for you. What remains is providing a mechanism for users to set a Book 's authors, and an Author 's books. And, we created a public Authors property that serves as a proxy, pulling this book's authors from BookAuthors :. The problem with this approach is we now have no way to tell when a caller adds or removes Author s from our Book.

What we need is something akin to the EntityRef delegates for OnAdd and OnRemove so that we can perform our synchronization whenever a caller adds or removes an author. Since our data is not in an EntryRef, we need another way to get notifications when an author is added or deleted.

We can do this by making Authors return an ObservableCollection that will notify us when changes are made to it:. The second line authors. This is the delegate method we registered to be notified of all changes made to our Authors collection.

Create the method to respond to the Add and Remove events, and use its NotifyCollectionChangedEventArgs to gain access to the items that were added e. NewItems or removed e. OldItems :. If you've been coding along, then you already did this when you added logic to BookAuthor 's set methods for Book to add the new BookAuthor instance to the Book :. The way this works is that when you attach add the BookAuthor instance to your Book , LINQ to SQL notices the change because it's monitoring Book for changes , and so it will automatically insert this attached BookAuthor record into the database the next time you call SubmitChanges.

Voila, a new BookAuthor relationship. Step 1 calls a RemoveRecord method on the DataContext which we'll add below. The reason it does this is because, unlike any of our other changes made so far, deleting a record always requires direct access to a DataContext. Asked 8 years ago. Active 10 months ago. Viewed k times. I need to set a value in a table for a subset of rows. Is this the most efficient way, if this is even possible? Improve this question. Craig Craig Add a comment.

Active Oldest Votes. Persons where p. SingleOrDefault ; result. Persons where SaveChanges ;. Improve this answer. This worked best. Craig — user It is necessary! Is there a reason for the List and ForEach this is not necessary? Please Sign up or sign in to vote. Question Can I update my employee record as given in the function below or do I have to make a query of the employee collection first and then update the data?

Copy Code. Pranay Rana. Software Developer Senior. Web development in Asp. ASh ASh Union myList. Community Bot 1 1 1 silver badge. Final Nemael Final Nemael 13 3 3 bronze badges.

The other answers already made this point. I don't think your answer adds much to this. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related Hot Network Questions. Question feed.

Stack Overflow works best with JavaScript enabled.



0コメント

  • 1000 / 1000