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. manjaro中文输入法已安装但切换不了解决方法

    情况如图所示,输入法安装了,但Ctrl+空格键或者鼠标选择切换都不行 解决方法: 打开家目录下面的.xprofile文件,如果没有这个文件就新建一个,加入下面内容 保存文件,退出. 重启电脑就可以了

  2. Python虚拟机类机制之填充tp_dict(二)

    填充tp_dict 在Python虚拟机类机制之对象模型(一)这一章中,我们介绍了Python的内置类型type如果要完成到class对象的转变,有一个重要的步骤就是填充tp_dict对象,这是一个极 ...

  3. 洛谷P1424小鱼的航程改进版

    题目链接https://www.luogu.org/problemnew/show/P1424

  4. 介绍 Active Directory 域服务 (AD DS) 虚拟化

    TechNet 库 Windows Server Windows Server 2012 R2 和 Windows Server 2012 服务器角色和技术 Active Directory Acti ...

  5. DEDE调用指定文章ID来调用特定文档

    http://www.jb51.net/cms/137423.html 代码如下: {dede:arclist row=1 idlist='6'} <li><a href=" ...

  6. Careercup - Microsoft面试题 - 5917873302142976

    2014-05-12 06:56 题目链接 原题: A link list contains following elements struct node{ int data; node* next; ...

  7. 报错: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: An attempt by a client to checkout a Connection has timed out. 数据库连接超时

    解决方法一: [oracle@data ~]$ sqlplus / as sysdba——连接到数据库 SQL*Plus: Release 11.2.0.4.0 Production on Mon M ...

  8. Android坐标getLeft,getRight,getTop,getBottom,getLocationInWindow和getLocationOnScreen

    Android中获取坐标点的一些方法解释 一.getLocationInWindow和getLocationOnScreen的区别 // location [0]--->x坐标,location ...

  9. [转载]用等高线图(Contour maps)可视化多变量函数

    https://blog.csdn.net/xlinsist/article/details/50920479 Overview 由于我们用手来画三维图像很困难,我们可以用等高线图来描述图像会更加简单 ...

  10. java 四舍五入 保留两位小数

    1. 格式化字符串 java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00"); float val=Flo ...