Java教程

完善EF框架

本文主要是介绍完善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框架的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!