zoukankan      html  css  js  c++  java
  • Linq To Entity学习实践

     1     public class CustomDataContext<TEntity> : System.Data.Linq.DataContext
     2         where TEntity : class,new()
     3     {
     4         public CustomDataContext() :
     5             base(ConfigurationManager.AppSettings["TestDbConnectionString"])
     6         {
     7         }
     8 
     9         #region 创建一个新对象
    10         public bool Create(TEntity entity)
    11         {
    12             EntityList.InsertOnSubmit(entity);
    13             SubmitChanges();
    14             return true;
    15         }
    16 
    17         public bool Create(IEnumerable<TEntity> entities)
    18         {
    19             EntityList.InsertAllOnSubmit(entities);
    20             SubmitChanges();
    21             return true;
    22         }
    23         #endregion
    24 
    25         #region 删除一个对象
    26         public bool Delete(TEntity entity)
    27         {
    28             EntityList.DeleteOnSubmit(entity);
    29             SubmitChanges();
    30             return true;
    31         }
    32 
    33         public bool Delete(IEnumerable<TEntity> entities)
    34         {
    35             EntityList.DeleteAllOnSubmit(entities);
    36             SubmitChanges();
    37             return true;
    38         }
    39         #endregion
    40 
    41         #region 修改对象
    42         public bool Modify(TEntity entity)
    43         {
    44             EntityList.Attach(entity, true);
    45             SubmitChanges();
    46             return true;
    47         }
    48 
    49         public bool Modify(IEnumerable<TEntity> entities)
    50         {
    51             EntityList.AttachAll(entities, true);
    52             SubmitChanges();
    53             return true;
    54         }
    55         #endregion
    56 
    57         #region 查询对象
    58         public TEntity GetEntity(Expression<Func<TEntity, bool>> predicate)
    59         {
    60             return EntityList.FirstOrDefault(predicate);
    61         }
    62         public IList<TEntity> GetList()
    63         {
    64             return EntityList.ToList();
    65         }
    66         public IQueryable<TEntity> GetList(Expression<Func<TEntity, bool>> predicate)
    67         {
    68             return EntityList.Where(predicate);
    69         }
    70         #endregion
    71 
    72         public System.Data.Linq.Table<TEntity> EntityList
    73         {
    74             get
    75             {
    76                 return this.GetTable<TEntity>();
    77             }
    78         }
    79     }
    CustomDataContext
     1     [Table(Name = "dbo.DemoClass")]
     2     public class DemoClass
     3     {
     4         public DemoClass()
     5         {
     6         }
     7 
     8         [Column(Name = "ID", IsPrimaryKey = true, IsDbGenerated = true)]
     9         public int ID
    10         {
    11             get;
    12             set;
    13         }
    14 
    15         [Column(Name = "UserName")]
    16         public string UserName
    17         {
    18             get;
    19             set;
    20         }
    21 
    22         [Column(Name = "Phone")]
    23         public string Phone
    24         {
    25             get;
    26             set;
    27         }
    28 
    29         [Column(Name = "Email")]
    30         public string Email
    31         {
    32             get;
    33             set;
    34         }
    35 
    36         [Column(Name = "Password")]
    37         public string Password
    38         {
    39             get;
    40             set;
    41         }
    42     }
    DemoClass
     1             DemoClass model = new DemoClass();
     2             model.UserName = "zhagnsan";
     3             model.Phone = "12345678901";
     4             model.Email = "zhangsan@163.com";
     5             model.Password = "123";
     6             using (var context = new CustomDataContext<DemoClass>())
     7             {
     8                 if (model != null)
     9                 {
    10                     context.Create(model);
    11                 }
    12             }
    测试
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.Linq.Mapping;
    using System.Linq;
    using System.Linq.Expressions;
  • 相关阅读:
    java——testNG——工作复习——xml详解
    转义符,re模块,rangdom随机数模块,
    nyoj 814又见拦截导弹
    Soj题目分类
    Xcode 性能优化
    python使用pip安装模块出现ReadTimeoutError: HTTPSConnectionPool的解决方法(pip使用豆瓣源)
    浅谈模拟退火
    43-正则表达式(1)
    命令行上的narrowing(随着输入逐步减少备选项)工具
    有效决策,这么做就对了!
  • 原文地址:https://www.cnblogs.com/ziranquliu/p/4767185.html
Copyright © 2011-2022 走看看