[英]Entity Framework database creation C#
我在获取使用实体框架创建数据库的代码时遇到了一些麻烦。 不知道如何或为什么,但我想我错过了一些东西。
安全数据层.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;
程序.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");
}
}
}
如果有人可以帮助我弄清楚如何建立数据库连接以及将它们放在哪里,那将不胜感激。
您应该将连接字符串添加到 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; }
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.