Source Code

Entity Framework 5 Code First Relationships


The Professor solves a spelling problem.

After lunch, (deviled eggs and radishes for the Professor, which made Edward gag and silently thank his mother for her peanut butter and kiwi sandwich), the Professor continued discussing umbrellas.

I’m afraid, [he said clearly, despite the radish in his mouth], I’ve deceived you. While it’s true that an umbrella changes color depending on who uses it, we do not store the color as a word. That is, not as a string. We tried that, but the umbrellas would misspell the color names. Another mystery that Mrs. Gamp assures me she’ll solve some day.

To keep my own sanity, if not anyone else’s, in the face of the color blue being spelled “blew,” we required the umbrellas to choose from a list of colors. An enumeration. This has only been possible recently, and is quite practical and intuitive despite the challenges to the Framework designers.

For our example, we’ll say an umbrella can be one of three colors.* Here is the updated class schema.


public class PersonUmbrella
    //Primary Key
    [Key, Column(Order = 0)]
    public int PersonId { get; set; }
    [Key, Column(Order = 1)]
    public int UmbrellaId { get; set; }

    public UmbrellaColors Color { get; set; }

    public virtual Person Person { get; set; }
    public virtual Umbrella Umbrella { get; set; }

public enum UmbrellaColors

Notice we have changed the Color property to use an Enum. Now, we change the code.


PersonUmbrella personUmbrella = new PersonUmbrella()
    Person = edward,
    Umbrella = umbrella,
    Color = "Red"
Console.WriteLine("Jane uses it, too.");
jane.PersonUmbrellas.Add(new PersonUmbrella()
    Umbrella = umbrella,
    Color = "Green"

And that is all! We run the program, which produces exactly the same results.

Create Person without a name.
Saving changes.
  ERROR: The Name field is required.
  ERROR: The ReadingRoom field is required.
Add the required name and Library Card.
Add the Reading Room and save.
Saving changes.
Successful save.
Get the record we just saved, and display.
Person: Edward Farley has card: 123 and Reading Room: Lewis Wing Floor -3
Create and save Magnifier with required serial number
Saving changes.
Successful save.
Retrieve Magnifier, show it doesn't belong to someone.
Magnifier: Bar123, Person: available
Edward gets the Magnifier. Save and retrieve.
Saving changes.
Successful save.
Magnifier: Bar123, Person: Edward Farley
Try to add duplicate magnifier
Saving changes.
  ERROR: Magnifier with this serial nbr already exists.
Add/save Pencil setting Nickname to null.
Saving changes.
  ERROR: The Nickname field is required.
Set Nickname to empty string and save
Saving changes.
Successful save.
Retrieve pencil, change Nickname, save
Pencil: , Person: Edward Farley
Saving changes.
Successful save.
Pencil: Blackwing, Person: Edward Farley
Attempt adding Bicycle without a Person
Saving changes.
  ERROR: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Bicycles_dbo.Persons_PersonId". The conflict occurred in database "EFDb", table "dbo.Persons", column 'PersonId'.
The statement has been terminated.
Set the Person, save, retrieve and show
Saving changes.
Successful save.
Model: Herman, Person: Edward Farley
Add two books and save
Saving changes.
Successful save.
Add another person and save
Saving changes.
Successful save.
Each person has read both books
Saving changes.
Successful save.
Edward Farley's books: The House of Arden, Harding's Luck
Jane Eager's books: The House of Arden, Harding's Luck
Create an umbrella for Edward
Jane uses it, too.
Saving changes.
Successful save.
PersonUmbrellas: 2
Edward's Umbrella: Red
Jane's Umbrella: Green

Our table has changed, though. We now store the integer value of the Enum.


create table PersonUmbrellas (
 PersonId int not null foreign key references Persons(PersonId),
 UmbrellaId int not null foreign key references Umbrella(UmbrellaId),
 Color int not null,
 constraint pkPersonUmbrella primary key (PersonId, UmbrellaId)


*In reality, Mrs. Gamp’s umbrellas could be one of three hundred colors, and counting....

Enum Support (Code First) in Entity Framework 5
Enumeration Support in Entity Framework