So its important that you dispose it when youre done

Info icon This preview shows pages 577–579. Sign up to view the full content.

to tidy up once you no longer need the state it builds up. So it’s important that you dispose it when you’re done with it. The object context’s type here is AdventureWorksLT2008Entities . By default, Visual Studio will just append the word Entities to your database connection name. You can change this by selecting the EntityContainer item in the Model Browser—you can see this in the middle of Figure 14-3 —and then use the Properties panel to choose its name. But we’ll keep the default name in the examples. The Entity Data Model | 553
Image of page 577

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Notice that the LINQ query in Example 14-3 uses the context’s SalesOrderHeaders property as the query source. That’s not quite the same as the table name—the wizard has added an s . By default, the Entity Framework wizard will attempt to pluralize and depluralize words as appropriate—in general, it gives entity types singular names while properties that return collections of entities are plural. (The names in our conceptual model can differ slightly from our storage model thanks to the Entity Data Model’s mapping.) If you don’t like this plural handling, there’s a checkbox to turn it off when you import tables with the wizard. Example 14-3 also uses the SalesOrderHeader class generated for the entity type of the same name. The order range variable in the LINQ query is of this type, as is the order iteration variable in the loop. It’s this generated entity class that enables us to refer to database columns using normal C# syntax. The LINQ query’s where clause uses that entity class’s OrderDate property to build a query that uses the OrderDate column of the corresponding database table. Likewise, the loop uses normal C# property syntax to retrieve TotalDue , which repre- sents the column of the same name in the database. If this seems rather uneventful, well, that’s the idea. Compare this to the much more fiddly code in Example 14-1 —by mapping database columns to properties, the Entity Framework reduces the amount of friction involved in writing data access code. You can find the generated source code for the entities in the Solution Explorer by expanding the .edmx file—you’ll find a file with a similar name, but with .Designer.cs in place of .edmx (so AdventureWorks.De- signer.cs in this case). As with all generated code you should avoid mod- ifying it—Visual Studio tends to regenerate code from scratch each time any setting changes. But if you’d like to add features to these generated classes, that’s easily done—all the classes are generated with the partial keyword, meaning that you can put additional members in sep- arate source files. You can add another class definition with the same name as an entity type, marked with the partial keyword, to any source file. The C# compiler will effectively merge your partial class with the generated partial class.
Image of page 578
Image of page 579
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern