You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.1 KiB
75 lines
2.1 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.Entity;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace TinyPosEntity
|
|
{
|
|
public class EntityController
|
|
{
|
|
public static string ConnectionString { get; set; }
|
|
|
|
private static EntityController instance;
|
|
|
|
private EntityController(string connectionString)
|
|
{
|
|
EntityContext.ConnectionString = connectionString;
|
|
}
|
|
|
|
public static EntityController GetInstance()
|
|
{
|
|
if (string.IsNullOrEmpty(ConnectionString))
|
|
throw new Exception("ConnectionString 값이 필요합니다.");
|
|
|
|
if (instance == null)
|
|
instance = new EntityController(ConnectionString);
|
|
|
|
return instance;
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
Database.SetInitializer<EntityContext>(new EntityInitializer());
|
|
|
|
using (EntityContext context = new EntityContext())
|
|
{
|
|
context.Database.CreateIfNotExists();
|
|
}
|
|
}
|
|
|
|
public void AddStudent(Student student)
|
|
{
|
|
using (EntityContext context = new EntityContext())
|
|
{
|
|
context.Students.Add(student);
|
|
context.SaveChanges();
|
|
}
|
|
}
|
|
|
|
public List<Student> FindStudent(string name)
|
|
{
|
|
List<Student> result = null;
|
|
using (EntityContext context = new EntityContext())
|
|
{
|
|
var student = from s in context.Students
|
|
where s.StudentName.Equals(name)
|
|
select s;
|
|
|
|
if (student.Count() > 0)
|
|
result = student.ToList();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public void RemoveStudent(Student student)
|
|
{
|
|
using (EntityContext context = new EntityContext())
|
|
{
|
|
context.Database.ExecuteSqlCommand("DELETE FROM dbo.Students WHERE StudentName = {0}", student.StudentName);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|