03 managing relationships
TRANSCRIPT
![Page 1: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/1.jpg)
Click to edit Master subtitle style
03 | Managing Relationships
Adam Tuliper | Technical EvangelistChristopher Harrison | Content Developer
![Page 2: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/2.jpg)
Managing Relationships• One to Many Relationships• One to One Relationships• Many to Many Relationships
![Page 3: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/3.jpg)
Click to edit Master subtitle style
One to Many Relationships
![Page 4: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/4.jpg)
What have we seen so far?• "It just works" –Jon Galloway• Basic steps– Create a class– Add properties– Decorate with attributes as needed
![Page 5: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/5.jpg)
Class design
Album
TitleReleaseYear
Artist
NameBio
1
*
![Page 6: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/6.jpg)
So, if I wanted to create a relationship…• I should be able to just add properties, right?
![Page 7: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/7.jpg)
DEMOLet's just see what happens…
![Page 8: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/8.jpg)
Well, that didn't work!
![Page 9: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/9.jpg)
Why did we get a null reference error?• Entity Framework uses lazy loading by default• "Magic" to make this work uses dependency injection
& inheritance
![Page 10: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/10.jpg)
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);
![Page 11: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/11.jpg)
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
![Page 12: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/12.jpg)
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
![Page 13: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/13.jpg)
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
![Page 14: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/14.jpg)
DEMOOne-to-many relationships, for real
![Page 15: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/15.jpg)
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
![Page 16: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/16.jpg)
Click to edit Master subtitle style
One to one relationships
![Page 17: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/17.jpg)
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
![Page 18: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/18.jpg)
DEMOOne-to-one relationships
![Page 19: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/19.jpg)
Click to edit Master subtitle style
Many-to-many relationships
![Page 20: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/20.jpg)
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
![Page 21: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/21.jpg)
DEMOMany-to-many relationships
![Page 22: 03 managing relationships](https://reader036.vdocument.in/reader036/viewer/2022062522/58a63cb91a28ab68118b616d/html5/thumbnails/22.jpg)
©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.