0

I'm having a bit of trouble getting my code to create a database using Entity Framework. Not sure how or why, but I think I'm missing something.

SecurityDataLayer.cs

namespace SecurityDoorDatabase.DBConnection
{
    class SecurityDataLayer
    {
        public class SecurityDoorDBContext : DbContext
        {
            public DbSet<Person> Perons { get; set; }
            public DbSet<Employee> Employees { get; set; }
            public DbSet<Door> Doors { get; set; }
            public DbSet<DoorSecurity> DoorSecurities { get; set; }
            public DbSet<SecurityInput> SecurityInputs { get; set; }
            public DbSet<SecurityLevel> SecurityLevels { get; set; }
            public DbSet<SecurityCards> SecurityCard { get; set; }
            public DbSet<FingerPrints> FingerPrint { get; set; }
        }
    }
}

Example of one of the many CS files I'm using.

class SecurityCards
{
    int securityCardID;
    int securityCardScan;

    public int SecurityCardID { get => securityCardID; set => securityCardID = value; }
    public int SecurityCardScan { get => securityCardScan; set => securityCardScan = value; }

    public SecurityCards(int securityCardID, int securityCardScan)
    {
        SecurityCardID = securityCardID;
        SecurityCardScan = securityCardScan;
    }
}

The usings for every CS file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.Common;
using System.Collections.ObjectModel;

Program.CS

class Program
{
    static void Main(string[] args)
    {
        Employee dingleberrySmith = new Employee(0, "Dingleberry", "Smithson", "Smith", 0, "IT Tech",
            new FingerPrints(0,134), new SecurityCards(0,134), new SecurityLevel(4, 7));
        Employee narsetSarkhan = new Employee(0, "Narset", "", "Sarkhan", 0, "Secretary",
            new FingerPrints(0, 0), new SecurityCards(0, 0), new SecurityLevel(1, 1));

        Door door597 = new Door(598, 597, "Fifth Floor", 598, 7, 7, "Door Code", "Security Card", "Finger Print");
        Door door227 = new Door(228, 227, "Second Floor", 228, 2, 2, "Door Code", "Security Card", "Finger Print");
        Door doorEntrance = new Door(0, 0, "Lobby", 0, 0, 0, "", "", "");
        CanEmployeeGoThroughDoor(dingleberrySmith, door227);
        CanEmployeeGoThroughDoor(dingleberrySmith, door597);
        CanEmployeeGoThroughDoor(narsetSarkhan, door597);
        CanEmployeeGoThroughDoor(narsetSarkhan, doorEntrance);

        Console.ReadLine();
    }

    public static void CanEmployeeGoThroughDoor(Employee employee, Door door)
    {
        if ((employee.EmployeeSecurityLevel.SecurityLevelID >= door.SecurityLevelID) ||(employee.EmployeeSecurityLevel.SecurityLevelID >= door.SecurityLevelID && employee.EmployeeFingerPrint.FingerPrintScan > 0 && door.SecurityInputTertiary == "Finger Print" && employee.EmployeeSecurityCard.SecurityCardScan > 0 && door.SecurityInputSecondary == "Security Card") || (employee.EmployeeSecurityLevel.SecurityLevelID >= door.SecurityLevelID&& employee.EmployeeSecurityCard.SecurityCardScan > 0 && door.SecurityInputSecondary == "Security Card") || (employee.EmployeeSecurityLevel.SecurityLevelID >= door.SecurityLevelID && employee.EmployeeFingerPrint.FingerPrintScan > 0 && door.SecurityInputTertiary == "Finger Print"))
        {
            Console.WriteLine("Access Granted");
        }
        else
        {
            Console.WriteLine("Access Denied");
        }
    }
}

If someone could help me figure out how to make the database connections and where to put them, that'd be most appreciated.

1 Answer 1

1

You should add connection string to app.config file or web.config file. I will explain for app.config file and for EF code first.

Add this into <configuration> on app.config file.

<connectionStrings>
        <add name="ConnectionStringName" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=DbName; Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>

And your SecurityDataLayer.cs should be like:

namespace SecurityDoorDatabase.DBConnection
{
    class SecurityDataLayer
    {
        public class SecurityDoorDBContext : DbContext
        {
            public SecurityDoorDBContext() : base("name=ConnectionStringName")
            {
            }

            public DbSet<Person> Perons { get; set; }
            public DbSet<Employee> Employees { get; set; }
            public DbSet<Door> Doors { get; set; }
            public DbSet<DoorSecurity> DoorSecurities { get; set; }
            public DbSet<SecurityInput> SecurityInputs { get; set; }
            public DbSet<SecurityLevel> SecurityLevels { get; set; }
            public DbSet<SecurityCards> SecurityCard { get; set; }
            public DbSet<FingerPrints> FingerPrint { get; set; }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.