EF框架--数据库增删改查泛型化
2016-07-18 00:00
316 查看
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Tools
{
//添加实体
public static Boolean AddEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
//db.Entry(t).State = EntityState.Added;
db.Set<T>().Add(t);
if(db.SaveChanges()!=0)
return true;
}
return false;
}
//添加实体s
public static bool AddEntities<T>(IEnumerable<T> entities) where T:class
{
using (var db = new MyContext())
{
db.Set<T>().AddRange(entities);
if (db.SaveChanges() !=0)
{
return true;
}
}
return false;
}
//删除实体
public static bool RemoveEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
//db.Entry(t).State = EntityState.Deleted;
db.Set<T>().Remove(t);
if(db.SaveChanges()!=0)
return true;
}
return false;
}
//更新实体
public static bool UpdateEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
db.Entry(t).State = EntityState.Modified;
if(db.SaveChanges()!=0)
{
return true;
}
}
return false;
}
//查询 实体
public static T QueryEntity<T>( params object[] ob) where T : class
{
using (var db = new MyContext())
{
return db.Set<T>().Find(ob);
}
}
}
}
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Tools
{
//添加实体
public static Boolean AddEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
//db.Entry(t).State = EntityState.Added;
db.Set<T>().Add(t);
if(db.SaveChanges()!=0)
return true;
}
return false;
}
//添加实体s
public static bool AddEntities<T>(IEnumerable<T> entities) where T:class
{
using (var db = new MyContext())
{
db.Set<T>().AddRange(entities);
if (db.SaveChanges() !=0)
{
return true;
}
}
return false;
}
//删除实体
public static bool RemoveEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
//db.Entry(t).State = EntityState.Deleted;
db.Set<T>().Remove(t);
if(db.SaveChanges()!=0)
return true;
}
return false;
}
//更新实体
public static bool UpdateEntity<T>(T t) where T : class
{
using (var db = new MyContext())
{
db.Entry(t).State = EntityState.Modified;
if(db.SaveChanges()!=0)
{
return true;
}
}
return false;
}
//查询 实体
public static T QueryEntity<T>( params object[] ob) where T : class
{
using (var db = new MyContext())
{
return db.Set<T>().Find(ob);
}
}
}
}
相关文章推荐
- 在EF中使用MySQL的方法及常见问题
- ASP.NET Mvc开发之EF延迟加载
- EntityFramework
- 在EF中使用MySQL的方法及常见问题
- Linux中如何杀掉僵尸进程
- Entity Framework数据库初始化四种策略
- 使用EF框架的增删改查和分页的公共类
- 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性。
- EF(EntityFramework) 的 CodeFirst 使用指南一(创建)
- EF(EntityFramework) 的 CodeFirst 使用指南二(基本使用)
- entity framework的配置步骤
- (无法推断有效的主键。已排除该表/视图)Entity Framework 无法对没有主键的视图映射实体的解决办法
- MVC中“The model backing the '***Context' context has changed since the database was created. ”的问题处理
- Linq To Entity 的增删改查(转)
- 关于EF学习,数据增删改查的讲解
- vs2012通过EF连接mysql数据库全程
- EF code-first 数据迁移
- 更新条目时出错。有关详细信息,请参见内部异常 错误原因
- 大数据量数据存储分表实例(企业级应用系统)附原码
- EF6.0 生成的代码中没有注释的解决方法