public class CustomDataContext<TEntity> : System.Data.Linq.DataContext
where TEntity : class,new()
{
public CustomDataContext() :
base(ConfigurationManager.AppSettings["TestDbConnectionString"])
{
} #region 创建一个新对象
public bool Create(TEntity entity)
{
EntityList.InsertOnSubmit(entity);
SubmitChanges();
return true;
} public bool Create(IEnumerable<TEntity> entities)
{
EntityList.InsertAllOnSubmit(entities);
SubmitChanges();
return true;
}
#endregion #region 删除一个对象
public bool Delete(TEntity entity)
{
EntityList.DeleteOnSubmit(entity);
SubmitChanges();
return true;
} public bool Delete(IEnumerable<TEntity> entities)
{
EntityList.DeleteAllOnSubmit(entities);
SubmitChanges();
return true;
}
#endregion #region 修改对象
public bool Modify(TEntity entity)
{
EntityList.Attach(entity, true);
SubmitChanges();
return true;
} public bool Modify(IEnumerable<TEntity> entities)
{
EntityList.AttachAll(entities, true);
SubmitChanges();
return true;
}
#endregion #region 查询对象
public TEntity GetEntity(Expression<Func<TEntity, bool>> predicate)
{
return EntityList.FirstOrDefault(predicate);
}
public IList<TEntity> GetList()
{
return EntityList.ToList();
}
public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> predicate)
{
return EntityList.Where(predicate);
}
#endregion public System.Data.Linq.Table<TEntity> EntityList
{
get
{
return this.GetTable<TEntity>();
}
}
}

CustomDataContext

     [Table(Name = "dbo.DemoClass")]
public class DemoClass
{
public DemoClass()
{
} [Column(Name = "ID", IsPrimaryKey = true, IsDbGenerated = true)]
public int ID
{
get;
set;
} [Column(Name = "UserName")]
public string UserName
{
get;
set;
} [Column(Name = "Phone")]
public string Phone
{
get;
set;
} [Column(Name = "Email")]
public string Email
{
get;
set;
} [Column(Name = "Password")]
public string Password
{
get;
set;
}
}

DemoClass

             DemoClass model = new DemoClass();
model.UserName = "zhagnsan";
model.Phone = "";
model.Email = "zhangsan@163.com";
model.Password = "";
using (var context = new CustomDataContext<DemoClass>())
{
if (model != null)
{
context.Create(model);
}
}

测试

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Linq.Expressions;

最新文章

  1. 使用rest方式修改服务端xml文件
  2. BZOJ1588[HNOI2002]营业额统计
  3. Java入门1day
  4. 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏
  5. javase建议学习路线
  6. shell编程之echo命令
  7. Physicals
  8. dmalloc 原文 翻译整理
  9. ASP.NET 运行机制
  10. Android问题-打开DelphiXE8与DelphiXE10编译空工程提示“[Exec Error] The command exited with code 1.”
  11. Red and Black
  12. Node.js事件发射器
  13. Log4j使用教程 (转载http://www.codeceo.com/article/log4j-usage.html)
  14. 条件注释判断IE版本
  15. 深入剖析kafka架构内部原理
  16. 【sqli-labs】Less18~Less22
  17. 创业维艰--&gt;&gt;书摘+乱七八糟
  18. HTTP数据组织方式
  19. 破解WEP工具wesside-ng
  20. migrantion

热门文章

  1. python 时间加8小时后的时间
  2. python虚拟环境 virtualenv工具
  3. 六、Shell echo命令
  4. 用dump为什么总会在前面出现/path/debug.php:193
  5. win7旗舰版64位java的jdk环境变量的配置(2012-12-26-bd 写的日志迁移
  6. day09-函数讲解
  7. MySQL创建数据库及用户
  8. P1101 单词方阵
  9. Fiddler用AutoResponder实现app升级异步更新
  10. Kali 中文家目录改英文目录