Transcript
Click to edit Master subtitle style
03 | Managing Relationships
Adam Tuliper | Technical EvangelistChristopher Harrison | Content Developer
Managing Relationships• One to Many Relationships• One to One Relationships• Many to Many Relationships
Click to edit Master subtitle style
One to Many Relationships
What have we seen so far?• "It just works" –Jon Galloway• Basic steps– Create a class– Add properties– Decorate with attributes as needed
Class design
Album
TitleReleaseYear
Artist
NameBio
1
*
So, if I wanted to create a relationship…• I should be able to just add properties, right?
DEMOLet's just see what happens…
Well, that didn't work!
Why did we get a null reference error?• Entity Framework uses lazy loading by default• "Magic" to make this work uses dependency injection
& inheritance
Lazy loading in actionAlbum
Title : stringReleaseYear : int
Artist : Artist
1. When Album is loaded, all of the simple data types are retrieved
2. A stub is placed into all of the complex data types• This stub contains the logic to have its
data loaded when it's first used3. When a property or method is called from
the complex type, the request is then made to load the data
Album album = context.Find(42);Console.WriteLine(album.Title);
1. Console.WriteLine(album.Artist.Name);
How do we support lazy loading?• Mark all complex type properties as virtual
• Can I disable lazy loading?– Short answer: yes– Longer answer: need to tell EF what to load and when to
load it• Adam will talk about this later today
Is that all I need to know?• Well… Not really…
• When loaded the child property is null• If you save the object to the database, it will attempt
to save the object with a null property– This will raise a referential integrity error
How do I solve that?• Add a property for the key of the primary object– ArtistID for Artist
• Entity Framework will automatically pick this up based on convention– Use the ForeignKeyAttribute if you need to change the
name
DEMOOne-to-many relationships, for real
Final one-to-many relationship note• Cascade delete is set to true– Deleting an Artist deletes their albums– Can be changed with the Fluent API
Click to edit Master subtitle style
One to one relationships
One-to-one relationships aren't common• …and Entity Framework knows it• As a result, it's not expecting it– The default is one-to-many–When two classes point to one another, EF can't determine
which is the parent and which is the child• One-to-one relationships must be explicitly created– Add ForeignKey to the child class
DEMOOne-to-one relationships
Click to edit Master subtitle style
Many-to-many relationships
Many-to-many relationship concepts• Relational databases typically don't support many-to-
many relationships natively– Requires a "join table" be created
• Fortunately, EF knows this– Just add the properties to both sides– EF will create the join table
DEMOMany-to-many relationships
©2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, Office, Azure, System Center, Dynamics and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.
top related