让我的代码使用Entity Framework创建数据库时遇到麻烦。不知道如何或为什么,但是我想我缺少了一些东西。
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; }
}
}
}
我正在使用的许多CS文件之一的示例。
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;
}
}
每个CS文件的用途。
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");
}
}
}
如果有人可以帮助我弄清楚如何建立数据库连接以及将它们放在哪里,那将是万分感谢。
答案 0 :(得分:1)
您应该将连接字符串添加到app.config文件或web.config文件。我将首先解释app.config文件和EF代码。
将此添加到app.config文件的<configuration>
中。
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=DbName; Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
您的SecurityDataLayer.cs应该类似于:
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; }
}
}
}