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. 设计模式基础--Java接口和抽象类

    最近在看设计模式,感觉需要先好好区分下抽象类和接口. 一.抽象类 <Java编程思想>中这样定义:包含抽象方法的类叫做抽象类. 解释: 1.包含,说明抽象类中可以有其他的具体方法. 2.因 ...

  2. 解决国内网络Python2.X 3.X PIP安装模块连接超时问题

    pip国内的一些镜像   阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...

  3. 配置vim nginx.conf高亮

    #!/bin/bashwget http://www.vim.org/scripts/download_script.php?src_id=14376 -O nginx.vimmv nginx.vim ...

  4. GNU中的关键字typeof

    如果你是 C++ 程序员,应该接触过 C++11 里的 decltype 操作符,它的作用是自动推导表达式的数据类型,以解决泛型编程中有些类型由模板参数决定而难以(甚至不可能)表示的问题.其实这个特性 ...

  5. HDU1505-City Game(记忆化搜索)

    City Game http://acm.hdu.edu.cn/showproblem.php?pid=1505 Problem Description Bob is a strategy game ...

  6. V4L2学习(一)整体说明

    1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡是内核中的子系统都有抽象底层硬件的差异,为上层提供统一的接口和提取出公共代 ...

  7. 利用本地SQL Server维护计划来维护SQL Database

    On-Premise的SQL Server提供了维护计划来定期.定时的维护SQL Server.一般的做法是:定义SQL Server Agent Jobs,而后维护计划帮助我们定期.定时执行SQL ...

  8. cf965e Short Code

    ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...

  9. 对于xss等有关的html,url,unicode编码做的一个小总结。

    参考:http://bobao.360.cn/learning/detail/292.html,算是对前部分作一个总结性的学习. 1<a href="%6a%61%76%61%73%6 ...

  10. PostgreSQL 安装完成后的一些配置

    设置数据目录,一般使用环境变量 PGDATA 指向数据目录的根目录.目录的初始化是使用 initdb 来完成的,-D 后面加上自定义目录的路径 initdb -D /tmp/testpostgres2 ...