3) A Simple Table
4) One to One or Many
5) Zero or One to Zero or Many
6) One to Zero or Many
7) One to Zero or One
8) One to One
9) Many to Many, Simple
10) Many to Many with Payload
Entity Framework 5 Code First Relationships
When I started this series about Entity Framework, I needed a good metaphor for the database relationships. I decided on a library, but soon realized my examples were becoming contrived. I was “fitting facts to suit theories.”* And yet I liked the library story.
Imagination floated in--as she often does--and in that kind yet firm tone said, “Why constrain yourself to reality? Why not invent?”
I listened, and after assembling all the material, wrote the light story of of a boy, Edward Farley, and his desire to learn. Following Netflix’s example with House of Cards, I’m releasing all the parts at once.
What follows is an opinionated (in the positive sense) set of examples for creating EF 5 Code First Relationships. It’s for me, the way I like to do them, and if it helps you, wonderful.
There are some oblique database references, and more than a few links to people and things that interest me. I’ve tried hard to be error-free, but that is a path of folly.
Where Edward Farley begins his quest.
When Edward Farley turned twelve years old, he announced to his family, “I’m going to the library and find out how everything works.”
You might imagine that his parents would be surprised, or puzzled. But Edward had always been like this, wondering the how and why of things. In fact, his mother and father expected a day like this.
“Well, Edward, if you really want to know, then there’s only one person to ask,” said his father.
Edward nodded. “Which reading room is he in?”
A Simple Table
Professor Relvar lays the framework for learning the Framework.
First, Edward [said the Professor], we’re going to write some code that will make it easier to learn about the Framework. And we’ll always strive to be a simple as possible--but not simpler.* You’re familiar with Visual Studio, I assume? Good. We’ll create a Console Application and name it something whimsical. How about “GnomeMobile”? No? Well, then, we’ll name it “LearningEF.” Next, we’ll add the Entity Framework 5 NuGet package. We’ll use SQL Express, so that we can easily run queries using Enterprise Manager. Are you following? Good. Normally, I like a file per class, but today I feel abnormal, so we’ll put the entire schema in one file. Here is the class for our first table.
A very cool and interesting way to get an introduction to EF CF. Heck, you might want to read this just for the fun of reading it... :)