Skip to content

Database

Zach Sanford edited this page Nov 4, 2022 · 4 revisions

πŸ” Overview

The database is an in-memory database. The reason I did this is because I wanted a simple connection for demonstration purposes. It makes the process easier for the person viewing the project. I did use Entity Framework Core 6 to connect to it, which can be easily detached and attached to a live database.

🚧 Setting Up

File
~\Staffify\Database\DatabaseContext.cs

To setup the database, I created a Database Context based off of EFCore's DbContext. For the options, I configured the database name. From there I made the only "table" needed, Employees. This table is based off of the Employee Model.

// Build DB - Options
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    // Set DB name
    optionsBuilder.UseInMemoryDatabase(databaseName: "StaffifyDB");
}

// Tables
public DbSet<Employee>? Employees { get; set; }

πŸ”Œ Interface

File
~\Staffify\Database\IDatabase.cs

I created this interface because there might be other tables that we need to connect to and do various operations. This interface defines the base operations for inheriting classes. As of right now, there are only the needed operations per the assessment. These operations are read and update.

βš™οΈ Class

File
~\Staffify\Database\Database.cs

This is where I perform all of the database operations. First in the constructor I check to see if there is anything in the database. If there is nothing, I populate test data.

if (!_context.Employees.Any())
{
    List<Employee> _employeesToAdd = new()
    {
        new Employee
        {
            EmployeeId = 1,
            Name = "Jalen Hurts",
            PhoneNumber = "(123) 456-7890",
            EmailAddress = "jhurts@flyeaglesfly.com"
        },
        new Employee
        {
            EmployeeId = 11,
            Name = "A.J. Brown",
            PhoneNumber = "(123) 456-7891",
            EmailAddress = "abrown@flyeaglesfly.com"
        },
        new Employee
        {
            EmployeeId = 6,
            Name = "DeVonta Smith",
            PhoneNumber = "(123) 456-7892",
            EmailAddress = "dsmith@flyeaglesfly.com"
        },
        new Employee
        {
            EmployeeId = 91,
            Name = "Fletcher Cox",
            PhoneNumber = "(123) 456-7893",
            EmailAddress = "fcox@flyeaglesfly.com"
        }
    };

    _context.AddRange(_employeesToAdd);
    _context.SaveChanges();
}

From here, I implement the inherited methods from the interface using simple LINQ queries.

Staffify Wiki

  • πŸ§‘β€πŸ€β€πŸ§‘ Models

Clone this wiki locally