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