13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

318 CHAPTER 12 LINQ beyond collectionsList<strong>in</strong>g 12.1Add<strong>in</strong>g a constructor to the User entity classpublic partial class User{public User (str<strong>in</strong>g name, UserType userType): this(){Name = name;UserType = userType;}}It’s important to call the parameterless constructor from our new one—the generatedcode <strong>in</strong>itializes some members there. In the same way as we added a constructor, wecan override ToStr<strong>in</strong>g <strong>in</strong> all the entities, so that the results will be exactly as they were<strong>in</strong> chapter 11. The generated code conta<strong>in</strong>s many partial method declarations, so youcan easily react to various events occurr<strong>in</strong>g on particular <strong>in</strong>stances.At this po<strong>in</strong>t, we can simply copy over the SampleData.cs file from chapter 11 andbuild the project. Now for the tricky bit—copy<strong>in</strong>g the sample data <strong>in</strong>to the databasefrom the <strong>in</strong>-memory version.12.1.2 Populat<strong>in</strong>g the database with sample dataOK, I lied. Populat<strong>in</strong>g the database is ludicrously easy. List<strong>in</strong>g 12.2 shows just how simpleit is.List<strong>in</strong>g 12.2Populat<strong>in</strong>g the database with our sample dataus<strong>in</strong>g (var context = new DefectModelDataContext()){context.Log = Console.Out; C Enables console logg<strong>in</strong>g Bcontext.Users.InsertAllOnSubmit(SampleData.AllUsers);context.Projects.InsertAllOnSubmit(SampleData.AllProjects);context.Defects.InsertAllOnSubmit(SampleData.AllDefects);context.NotificationSubscriptions.InsertAllOnSubmit(SampleData.AllSubscriptions);Populatescontext.SubmitChanges();Flushes changes entities}to databaseECreatescontext towork <strong>in</strong>I’m sure you’ll agree that’s not a lot of code—but all of it is new. Let’s take it one stepat a time. First we create a new data context to work with B. Data contexts are prettymultifunctional, tak<strong>in</strong>g responsibility for connection and transaction management,query translation, track<strong>in</strong>g changes <strong>in</strong> entities, and deal<strong>in</strong>g with identity. For the purposesof this chapter, we can regard a data context as our po<strong>in</strong>t of contact with thedatabase. We won’t be look<strong>in</strong>g at the more advanced features here, but there’s oneuseful capability we’ll take advantage of: at C we tell the data context to write out allthe SQL commands it executes to the console.The four statements at D add the sample entities to the context. The four propertiesof the context (Users, Projects, Defects, and NotificationSubscriptions) areDLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!