03 managing relationships

22
Click to edit Master subtitle style 03 | Managing Relationships Adam Tuliper | Technical Evangelist Christopher Harrison | Content Developer

Upload: -

Post on 17-Feb-2017

1.634 views

Category:

Education


0 download

TRANSCRIPT

Page 1: 03   managing relationships

Click to edit Master subtitle style

03 | Managing Relationships

Adam Tuliper | Technical EvangelistChristopher Harrison | Content Developer

Page 2: 03   managing relationships

Managing Relationships• One to Many Relationships• One to One Relationships• Many to Many Relationships

Page 3: 03   managing relationships

Click to edit Master subtitle style

One to Many Relationships

Page 4: 03   managing relationships

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

Class design

Album

TitleReleaseYear

Artist

NameBio

1

*

Page 6: 03   managing relationships

So, if I wanted to create a relationship…• I should be able to just add properties, right?

Page 7: 03   managing relationships

DEMOLet's just see what happens…

Page 8: 03   managing relationships

Well, that didn't work!

Page 9: 03   managing relationships

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

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

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

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

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

DEMOOne-to-many relationships, for real

Page 15: 03   managing relationships

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

Click to edit Master subtitle style

One to one relationships

Page 17: 03   managing 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

Page 18: 03   managing relationships

DEMOOne-to-one relationships

Page 19: 03   managing relationships

Click to edit Master subtitle style

Many-to-many relationships

Page 20: 03   managing 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

Page 21: 03   managing relationships

DEMOMany-to-many relationships

Page 22: 03   managing 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.