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;

Linq To Entity学习实践的更多相关文章

  1. Linq学习<三> linq to entity

    之前一直用sql选择出数据放在一个集合中,然后再用Linq或者lambda去操作数据,今天学了Linq to entity 才知道原来linq产生是为了Entity.也就是EDM(实体数据模型) 关于 ...

  2. 第04项目:淘淘商城(SpringMVC+Spring+Mybatis) 的学习实践总结【第六天】

    https://pan.baidu.com/s/1bptYGAb#list/path=%2F&parentPath=%2Fsharelink389619878-229862621083040 ...

  3. 深入调研Linq to Objects Join Linq to Entity

    最近工作中遇到数据库组合查询带来的一些问题,因此有必要调研一下Linq to Objects Join Linq to Entity.参考一些网友的代码案例,深入实践了一下使用EntityFramew ...

  4. 使用sklearn进行集成学习——实践

    系列 <使用sklearn进行集成学习——理论> <使用sklearn进行集成学习——实践> 目录 1 Random Forest和Gradient Tree Boosting ...

  5. Nagios学习实践系列——基本安装篇

    开篇介绍 最近由于工作需要,学习研究了一下Nagios的安装.配置.使用,关于Nagios的介绍,可以参考我上篇随笔Nagios学习实践系列——产品介绍篇 实验环境 操作系统:Red Hat Ente ...

  6. Nagios学习实践系列——配置研究[监控当前服务器]

    其实上篇Nagios学习实践系列——基本安装篇只是安装了Nagios基本组件,虽然能够打开主页,但是如果不配置相关配置文件文件,那么左边菜单很多页面都打不开,相当于只是一个空壳子.接下来,我们来学习研 ...

  7. Linq to Entity经验:表达式转换

    http://www.cnblogs.com/ASPNET2008/archive/2012/10/27/2742434.html 最近一年的项目,我主要负责一些小型项目(就是指企业内部的小项目),在 ...

  8. 前端学习实践笔记--JavaScript深入【1】

    这一年中零零散散看过几本javascript的书,回过头看之前写过的javascript学习笔记,未免有点汗颜,突出“肤浅”二字,然越深入越觉得javascript的博大精深,有种只缘身在此山中的感觉 ...

  9. EF架构~linq to entity的随机排序问题

    回到目录 对于从linq to sql迁移过来的开发者,对随机排序不会感到陌生,直接为datacontext添加一个方法再配合反射就可以实现随机排序了,代码如下: /// <summary> ...

随机推荐

  1. python 使用requests 请求 https 接口 ,取消警告waring

    response = requests.request("POST", url, timeout=20, data=payload, headers=headers, proxie ...

  2. GSMM数据库设计小结

    边写边结 1.新增,删除,修改在各自的DAL中进行,查,可以新建一个DAL,里面是需要的各个属性,跨表,不同表属性整合成一个对象(集合)返回,输出到用户界面.

  3. python集成开发环境PyCharm

    环境安装视频介绍:http://pan.baidu.com/s/1gfz6wiZ ppmb 外加几个截图: activate:

  4. Compoer介绍

    Compoer介绍 Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 安装Composer Composer.phar 是 Compos ...

  5. {{}},ng-bind和ng-model的区别

    ng-bind 与ng-model区别 <input ng-model="object.xxx"> <span ng-bind="object.xxx& ...

  6. 云计算之路-阿里云上:在SLB上部署https遇到的问题及解决方法

    一.问题场景 这个问题只会出现在云服务器操作系统使用Windows Server 2012的场景,如果使用的是Windows Server 2008 R2则不存在这个问题. 二.https部署场景 1 ...

  7. jmeter将上一个请求的结果作为下一个请求的参数——使用正则提取器

    转自:http://www.cnblogs.com/0201zcr/p/5089620.html 在压力测试的时候,经常要将几个流程串联起来才能将程序测试通过.如:我现在用户首先要登录,获得我登录的凭 ...

  8. java线程安全问题原因及解决办法

    1.为什么会出现线程安全问题 计算机系统资源分配的单位为进程,同一个进程中允许多个线程并发执行,并且多个线程会共享进程范围内的资源:例如内存地址.当多个线程并发访问同一个内存地址并且内存地址保存的值是 ...

  9. Python 操作 SQLite 数据库

    写在之前 SQLite 是一个小型的关系型数据库,它最大的特点在于不需要单独的服务.零配置.我们在之前讲过的两个数据库,不管是 MySQL 还是 MongoDB,都需要我们安装.安装之后,然后运行起来 ...

  10. Java开发微信公众号(一)---初识微信公众号以及环境搭建

    ps:1.开发语言使用Java springMvc+Mybaits+spring maven实现 2.使用微信接口测试账号进行本地测试 https://mp.weixin.qq.com/debug/c ...