教学资料管理系统演示,仿照视频链接在最下方
第一天数据库建立、EF框架应用与BLL层创建
1.1 数据库建立
图1.1.1
注意主键的确认和标识规范
图1.1.2
默认值填写 getdate(),外面那个多余括号软件不知道怎么就怎么生成了
1.2 EF框架应用
图1.2.1
添加一个类库,删除创建时自带的cs文件,在里面创建ADO.NET 实体数据模型,如果提前写好数据库就从数据库导入,如果没写,创建空模型在C#直接写也一样。
1.3 BLL层创建
//创建一个泛型接口,进行增删改查
public interface IService<T> where T:class
{
//返回一个int类型,插入成功和失败
int Insert(T t);
//返回一个int类型,修改成功和失败
int Update(T t);
//返回一个int类型,删除成功和失败
int Delete(T t);
//查找返回是个集合
List<T> Select();
//int id 返回个T
T Select(int id);
}
using Models;
namespace BLL
{
//让这个类集成IService,正常创建类没有public,都加一下
//这个类是一个泛型类,这个Class是model1.tt下面的Class,但是直接写会报错,
//需要添加引用过来才行,1、BLL添加引用选Model,2、using models名称空间添加,3、显示可能修补程序选 实现接口
//代码自动就出来了
public class ClassService : IService<Class>
{
TeacherDocsEntities db = new TeacherDocsEntities();
public int Delete(Test t)
{
db.Entry(t).State = System.Data.Entity.EntityState.Deleted;
return db.SaveChanges();
// throw new NotImplementedException();
}
public int Insert(Class t)
{
db.Entry(t).State = System.Data.Entity.EntityState.Added;
return db.SaveChanges();
//方法未实现,抛出异常
//throw new NotImplementedException();
}
public List<Class> Select()
{
return db.Class.ToList();
// throw new NotImplementedException();
}
public Class Select(int id)
{
return db.Class.FirstOrDefault(item => item.Id == id);
// throw new NotImplementedException();
}
public int Update(Test t)
{
db.Entry(t).State = System.Data.Entity.EntityState.Modified;
return db.SaveChanges();
// throw new NotImplementedException();
}
}