1 / 35

Data Modeling Seminar

Data Modeling Seminar. February 18 , 2012 Lesson 6 Data Access Layering – Part 2 Entity Framework v4.0. What You Just Learned. What You Just Learned Microsoft built into Entity Framework . The Essentials.

willis
Download Presentation

Data Modeling Seminar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data ModelingSeminar February 18, 2012 Lesson 6 Data Access Layering – Part 2 Entity Framework v4.0

  2. What You Just Learned • What You Just Learned • Microsoft built into Entity Framework

  3. The Essentials • The Entity Framework bridges the gap between how developers commonly manipulate conceptual objects and the way data is actually stored (records in database tables).  • The technical term for a tool that provides this abstraction is object relational mapper (ORM).  • ORMs help developers be more efficient and focused.. • It also means that the code is more portable – switching database software requires changing a setting in the ORM, not a rewrite of the whole codebase to match the new database's dialect. 

  4. Getting Started • One of the nice features of using the Entity Framework is its out-of-the-box simplicity.  • It does not need a download, install, or patch anything to get started with my first EF app.  • Boot up VS 2010, open a new project, design entities, and write code, then off you go.  • Follows is a step-by-step walkthrough.  A simple events calendar.

  5. Walkthrough • Begin by opening Visual Studio 2010.

  6. Walkthrough • From the 'File' menu select 'New' >> 'Project'. 

  7. Walkthrough • Visual Studio will now set up a basic C# application

  8. Walkthrough • The next step is adding your entities to the project.  • To do this, we’ll need to add a new file to our project which will contain the specifications for our entities.  • Open up the Solution Explorer panel.  • Right-click on the name of your application • Click on the option 'Add' >> 'New Item'. • Select 'ADO.NET Entity Data Model'. 

  9. Walkthrough

  10. Walkthrough • If you are starting an application from scratch, select 'Empty model.' • This is known as the model-first approach, instead of the database-first approach • If there was already a database in place, then you would select the 'Generate from database' option • The Entity Framework would do its best to create models and associations that fit the data. 

  11. Walkthrough • Let’s start with the model-first approach.

  12. Walkthrough • This will now open a new file – a canvas onto which you can design your data model.  • Rather than having to directly write the underlying XML file to specify the models and properties, this designer is a pretty slick way to configure your models and associate them.  • You start with an empty canvas.

  13. Walkthrough

  14. Walkthrough • To add an entity, right-click on the design surface (the white canvas area) and click 'Add' >> 'Entity' from the context menu.  • Type a name for the entity, and notice how it automatically attempts to pluralize it in the EntitySet field.  • EntitySetsare collections of entities (that map to tables in the database), which allow you to add, delete, or update entities (rows in the database).  • Although the system usually gets the pluralizing right, you can tweak it as you please. 

  15. Walkthrough

  16. Walkthrough • The next step will be to add properties to that entity.  • Properties are essentially "instance data" in the lexicon of object-oriented programming or "columns" in database parlance.  • You can add a property to an entity by right-clicking the entity and selecting 'Add' >> 'Scalar Property'.  • When you add the property, you can then name it whatever makes sense. 

  17. Walkthrough

  18. Walkthrough • Add an association between the Event and the User to represent which User created an Event.  • To do this, right click anywhere on the design surface and select 'Add' >> 'Association'.

  19. Walkthrough To configure the association: • Select the two “ends” of the association, or the two entities you will be associating, and their multiplicity to match the association you have in mind.  • In this example, setting up a many-to-one association between an Event and its creator; each Event has only one creator, but each User may have created many Events.  • Name this “EventCreator” to be specific about what this association represents and avoid collisions or ambiguity.

  20. Walkthrough To configure the association: • Customize the navigation properties using terms that make sense relative to this relationship.  • In this example, use the terms ‘Creator’ and ‘CreatedEvents.’  • These are the property names used by one entity to get to the other.  • One would use Event.Creator to get the single User who created that event and User.CreatedEvents for a list of events created by a certain User

  21. Walkthrough

  22. Walkthrough

  23. Walkthrough • Once the entities are ready: • Right click the canvas and select 'Generate Database from Model.'

  24. Walkthrough

  25. Walkthrough • Now let’s create a model from an existing database. • Open up the Solution Explorer panel.  • Right-click on the name of your application • Click on the option 'Add' >> 'New Item'. • Select 'ADO.NET Entity Data Model'. 

  26. Walkthrough

  27. Walkthrough

  28. Walkthrough

  29. Walkthrough • After a small wait, you should now have a model of your database complete with all scalar properties, navigational properties, and associations.

  30. Walkthrough

  31. Walkthrough • Build Model Context ClassScheduleEntitiesctx = newClassScheduleEntities() • Access Entities in the Model Context • using (ClassScheduleEntitiesctx = newClassScheduleEntities()) • { • var courses = ctx.Courses.ToList(); • foreach (Course c in courses) • { • Console.WriteLine(c.CourseId.ToString()); • } • }

  32. Walkthrough This query retrieves all Courses andtheir related Department in a single query.  EF 4 introduces the ability to load related Entities “on demand”: • using (ClassScheduleEntitiesctx = newClassScheduleEntities()) • { • varcourses = ctx.Courses.ToList(); • intid = courses.First().Department.DepartmentId; • } Without lazy loading, the code above would throw a NullReferenceException because the Department was neither pulled in with Include(), nor loaded explicitly with Load().  EF 4 allows the code above to “automatically” query for the related Department if it is not already loaded.  Lazy loading is “on” by default.

  33. Walkthrough There’s a caveat here.  Take a gander: • using (ClassScheduleEntitiesctx = newClassScheduleEntities()) • { • var courses = ctx.Courses.ToList(); • foreach (Course c in courses) • { • Console.WriteLine(c.Department.DepartmentId.ToString()); • } • } This is a case where lazy loading can get you into trouble.  Suppose there were 100 Courses in the courses List<Course>.  The code above would result in 100 additional queries, one for each Course in the list.  This can get even worse in the case of one-to-many relationships.

  34. Questions

  35. Lab 5 • Entity Framework 4.0 Demonstration

More Related