13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

324 CHAPTER 12 LINQ beyond collections-- Generated SQLSELECT [t0].[Summary], [t1].[Name]FROM [dbo].[Defect] AS [t0]LEFT OUTER JOIN [dbo].[DefectUser] AS [t1]ON [t1].[UserID] = [t0].[AssignedToUserID]Of course, if you navigate via more properties, the jo<strong>in</strong>s get more and more complicated.I’m not go<strong>in</strong>g <strong>in</strong>to the details here—the important th<strong>in</strong>g is that LINQ to SQLhas to do a lot of analysis of the query expression to work out what SQL is required.Before we leave LINQ to SQL, I ought to show you one more feature. It’s part ofwhat you’d expect from any decent ORM system, but leav<strong>in</strong>g it out would just feelwrong. Let’s update some values <strong>in</strong> our database.12.1.4 Updat<strong>in</strong>g the databaseAlthough <strong>in</strong>sertions are straightforward, updates can be handled <strong>in</strong> a variety of ways,depend<strong>in</strong>g on how concurrency is configured. If you’ve done any serious databasework you’ll know that handl<strong>in</strong>g conflicts <strong>in</strong> updates from different users at the sametime is quite hairy—and I’m not go<strong>in</strong>g to open that particular can of worms here. I’lljust show you how easy it is to persist a changed entity when there are no conflicts.Let’s change the status of one of our defects, and its assignee, and that person’sname, all <strong>in</strong> one go. As it happens, I know that the defect with an ID of 1 (as createdon a clean system) is a bug that was created by Tim, and is currently <strong>in</strong> an “accepted”state, assigned to Darren. We’ll imag<strong>in</strong>e that Darren has now fixed the bug, andassigned it back to Tim. At the same time, Tim has decided he wants to be a bit moreformal, so we’ll change his name to Timothy. Oh, and we should remember to updatethe “last modified” field of the defect too. (In a real system, we’d probably handle thatwith a trigger—<strong>in</strong> LINQ to SQL we could implement partial methods to set the lastmodified time when any of the other fields changed. For the sake of simplicity here,we’ll do it manually.)List<strong>in</strong>g 12.5 accomplishes all of this and shows the result—load<strong>in</strong>g it <strong>in</strong> a freshDataContext to show that it has gone back to the database.List<strong>in</strong>g 12.5Updat<strong>in</strong>g a defect and show<strong>in</strong>g the new detailsus<strong>in</strong>g (var context = new DefectModelDataContext()){context.Log = Console.Out;Defect defect = context.Defects.Where(d => d.ID==1).S<strong>in</strong>gle();BF<strong>in</strong>ds defect withextension methodsUser tim = defect.CreatedBy;}defect.AssignedTo = tim;tim.Name = "Timothy Trotter";defect.Status = Status.Fixed;defect.LastModified = SampleData.August(31);context.SubmitChanges();C Updatesentity detailsD Submits changes to databaseLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!