完善EF框架

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace DAL
{
//泛型 T:你传什么类型就是什么类型
public class BaseDAL<T> where T:BaseEntity
{
//private static Model1 _model = null;
/// <summary>
/// 数据上下文类
/// </summary>
public Model1 Model
{
get
{
return new Model1();
//if (_model==null)
//{
// _model = new Model1();
//}
//return _model;
}
}
/// <summary>
/// 查询集合
/// </summary>
/// <param name="Expression">表达式</param>
/// <returns>List集合</returns>
public List<T> Select(Expression<Func<T,bool>>Expression)
{
return Model.Set<T>().Where(Expression).ToList();
}
/// <summary>
/// 查询单个
/// </summary>
/// <param name="expression">表达式</param>
/// <returns>单个对象或null</returns>
public T Single(Expression<Func<T,bool>>expression)
{
return Model.Set<T>().FirstOrDefault(expression);
}
/// <summary>
/// 查询单个对象
/// </summary>
/// <param name="id">主键</param>
/// <returns>单个对象或null</returns>
public T Single(int id)
{
return Model.Set<T>().Find(id);
}
/// <summary>
/// 添加
/// </summary>
/// <param name="info">数据</param>
/// <returns>受影响的行数</returns>
public int Insert(T info)
{
Model.Set<T>().Add(info);
return Model.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="info">数据</param>
/// <returns>受影响的行数</returns>
public int Delete(T info)
{
var model = Model;
var temp = model.Set<T>().FirstOrDefault(p => p.Id == info.Id);
model.Set<T>().Remove(temp);
//var model = Model;
//model.Entry<T>(info).State = System.Data.Entity.EntityState.Deleted;

return model.SaveChanges();

}
}
}

 

 

 

 

 

 

 

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
namespace test
{
/// <summary>
/// DelClass 的摘要说明
/// </summary>
public class DelClass : IHttpHandler
{

ClassInfoBLL bll = new ClassInfoBLL();
public void ProcessRequest(HttpContext context)
{
var id = context.Request.Form["cid"];
if (string .IsNullOrWhiteSpace(id))
{
context.Response.Write("参数丢失");
}
else
{
var cid = Convert.ToInt32(id);
if(bll.Delete(new Model.ClassInfo() { Id = cid }) > 0)
{
context.Response.Write(1);
}
else
{
context.Response.Write(0);
}
}
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

上一篇:EF CodeFirst 入门 for oracle


下一篇:linux 命令输出作为另一个命令输入 cd