CREATE DATA MODEL CLASSES TO REPRESENT TRIPS AND RESERVATIONS, IMPLEMENT A DBCONTEXT-DERIVED CLASS, AND CREATE A NEW REPOSITORY CLASS FOR THE RESERVATION ENTITY.
To implement the required functionality, you need to perform the following tasks:
1. Explore the existing Entity framework data model project.
2. Create new data model classes.
3. Implement a data context by deriving from the Dbcontext class.
4. Create a new repository for the Reservation entity.
Task 1: Exploring the Existing Entity Framework Data Model Project
1. Browse to the location where Exercise 01 .zip file is saved.
2. Extract the files.
3. Double-click the Exercise 01 folder.
4. Double-click the BlueYonder.Companion folder.
5. Double-click the BlueYonder.Companion.sln file. The BlueYonder.Companion - Microsoft Visual Studio window displayed.
6. Ensure that the Solution Explorer window is opened.
7. Ensure that the BlueYonder.Entities node is expanded.
8. Double-click the FlightSchedule.es file. The FlightSchedule.es file is displayed.
9. Explore the properties of the FlightSchedule class. Explore how the DatabaseGenerated and ForeignKey attributes used in this class.
10. Ensure that the BlueYonder.DataAccess node is expanded in the Solution Explorer window.
11. Double-click the TravelCompanionContext.es file. The TravelCompanionContext.es file is displayed.
12. Explore the properties and methods of the TravelCompanionContext class. Explore the DbSet properties it contains.
13. Expand the Repositories node in the Solution Explorer window.
14. Double-click the FlightRepository.es file. The FlightRepository.es file is displayed.
15. Explore the methods of the FlightRepository class.
Task 2: Creating New Data Model Classes
1. Ensure that the Solution Explorer window is opened.
2. Right-click the BlueYonder.Entities node, and then select Add—Class. The Add New Item dialog box is displayed.
3. Select and replace the existing text in the Name text box with Trip.
4. Click the Add button. The Trip.cs file is displayed.
5. Type the highlighted portions of the following code snippet in the Trip.cs file: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations.Schema; using BlueYonder.Entities.Enums; namespace BlueYonder.Entities { public class Trip { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Tripld { get; set; } public int FlightSchedulelD { get; set; } [ForeignKey("FlightSchedulelD")] public virtual FlightSchedule Flightlnfo { get; set; } public FlightStatus Status { get; set; } public SeatClass Class { get; set; } > >
6. Select FILE— Save All to save the changes.
7. Ensure that the Solution Explorer window is opened.
8. Right-click the BlueYonder.Entities node, and then select Add—Class. The Add New Item - BlueYonder.Entities
dialog box is displayed.
9. Select and replace the existing text in the Name text box with Reservation.
10. Click the Add button. The Reservation.es file is displayed.
11. Type the highlighted portions of the following code snippet in the Reservation.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
namespace BlueYonder.Entities
{
public class Reservation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Reservations { get; set; } public int Travelerld { get; set; ) public DateTime ReservationDate { get; set; } public string ConfirmationCode { get; set; } public int DepartFlightSchedulelD { get; set; > public virtual Trip DepartureFlight { get; set; } public int? ReturnFlightSchedulelD { get; set; } public virtual Trip ReturnFlight { get; set; }
>
>
12. Select FILE—Save All to save the changes.
Task 3: Implementing a Data Context by Deriving from the DbContext Class
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.DataAccess node is expanded.
3. Double-click the TravelCompanionContext.es file. The TravelCompanionContext.es file is displayed.
4. Type the highlighted portion of the following code snippet in the TravelCompanionContext.es file:
public class TravelCompanionContext : DbContext
{
public DbSet<Location> Locations { get; set; }
public DbSet<Flight> Flights { get; set; }
public DbSet<FlightSchedule> FlightSchedules { get; set; }
public DbSet<Traveler> Travelers { get; set; }
public DbSet<Reservation> Reservations { get; set; }
5. Type the highlighted portions of the following code snippet inside the OnMcdelCreating() method:
protected override void 0nModelCreating(DbModel6uilder modelBuilder)
{
modelBuilder.Entity<FlightSchedule>()
.HasRequired(fs => fs.Flight)
.WithMany(f => f.Schedules)
.Map(m => m.MapKey("FlightID")); modelBuilder.Entity<Reservation>()
.HasRequired(r => r.DepartureFlight)
.WithMany()
.HasForeignKey(r => r.DepartFlightSchedulelD)j modelBuilder.Entity<Reservation>()
.HasOptional(r => r.ReturnFlight)
.WithMany()
.HasForeignKey(r => r.ReturnFlightSchedulelD)j
}
6. Select FILE— Save All to save the changes.
Task 4: Creating a New Repository for the Reservation Entity
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.DataAccess node is expanded.
3. Right-click the Repositories folder, and then select Add—Class. The Add New Item - BlueYonder.Access dialog box displayed.
4. Select and replace the existing text in the Name text box with ReservationRepository.
5. Click the Add button. The ReservationRepository.es file is displayed.
6. Type the highlighted portions of the following code snippet in the ReservationRepository.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq.Expressions;
using BlueYonder.DataAccess.Interfaces;
using BlueYonder.Entities;
namespace BlueYonder.DataAccess.Repositories
{
public class ReservationRepository : ISingleKeyEntityRepository<Reservation, int>
{
}
}
7. Type the highlighted portions of the following code snippet inside the ReservationRepository class:
public class ReservationRepository : ISingleKeyEntityRepository<Reservation, int>
{
TravelCompanionContext context} public ReservationRepository()
{
context = new TravelCompanionContext()}
}
public ReservationRepository(string connectionName)
{
context = new TravelCompanionContext(connectionName)}
}
public ReservationRepository(TravelCompanionContext dbContext)
{
context = dbContext}
}
public Reservation GetSingle(int entityKey)
{
var query = from r in context.Reservations
where r.Reservationld == entityKey select r}
return query.SingleOrOefault()}
}
public void Delete(Reservation entity)
{
entity =
context.Reservations.Find(entity.Reservationld)} if (entity.DepartFlightSchedulelD != 0)
context.Entry(entity.DepartureFlight).State = System.Data.EntityState.Deleted}
if (entity.ReturnFlightSchedulelD != 0)
context.Entry(entity.ReturnFlight).State = System.Data.EntityState.Deleted;
context.Reservations.Remove(entity);
}
public void Dispose()
{
if (context != null)
{
context.Di spose()j context = null;
}
GC.SuppressFinalize(this);
}
public IQueryable<Reservation> GetAllQ
{
return context.Reservations.AsQueryable<Reservation>();
}
public IQueryable<Reservation> FindBy(Expression<Func<Reservation, bool» predicate)
{
return GetAllQ .Where(predicate);
}
public void Add(Reservation entity)
{
context.Reservations.Add(entity);
}
public void Edit(Reservation entity)
{
var originalEntity = context.Reservations.Find(entity.Reservationld); context.Entry(originalEntity).CurrentValues.SetValues(entity);
}
public void SaveQ
{
context.SaveChanges();
}
}
8. Select FILE—Save All to save the changes.
9. Close Microsoft Visual Studio 2012.
No comments:
Post a Comment