O'Reilly logo

Programming Entity Framework: Code First by Rowan Miller, Julia Lerman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Putting the Pieces Together in an Example

To see all of this in action, the following section will walk you through creating a small solution in Visual Studio, where you’ll place these classes and then create a simplistic console application to exercise your new data layer. To be sure you’re starting on the right architectural path, this walkthrough will organize the layers of the application into separate projects.

  1. Create a new solution in Visual Studio.

  2. Add a Class Library project to the solution named Model.

  3. In this project, add a new class named Destination.

  4. Modify the Destination class to match the Example 2-3.

    Example 2-3. The Destination class

    using System.Collections.Generic;
    
    namespace Model
    {
        public class Destination
        {
            public int DestinationId { get; set; }
            public string Name { get; set; }
            public string Country { get; set; }
            public string Description { get; set; }
    
            public byte[] Photo { get; set; }
    
            public List<Lodging> Lodgings { get; set; }
        }
    }
  5. Add another class, named Lodging, to this project with the code shown in Example 2-4.

    Example 2-4. The Lodging class

    namespace Model
    {
        public class Lodging
        {
            public int LodgingId { get; set; }
    
            public string Name { get; set; }
            public string Owner { get; set; }
            public bool IsResort { get; set; }
    
            public Destination Destination { get; set; }
        }
    }

That’s the extent of the Model project. Now, on to the data layer. While the domain classes have no awareness of the Entity Framework, the data layer is completely dependent on it.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required