EF简单的增删查改

Add

 1   
 2         /// <summary>
 3         ///  
 4         /// </summary>
 5         public void Add()
 6         {
 7             TestDBEntities2 testdb = new TestDBEntities2();
 8            
 9             Test1 test1 = new Test1() { Name="yang"};
10 
11             testdb.Tests.Add(test1);
12 
13             testdb.SaveChanges();
14         }     
复制代码

Modify

复制代码
 1         public void Modify()
 2         { 
 3             //-------方法1--------
 4             // //1.先查询出来
 5             Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
 6             ////2修改内容
 7             test1.Name = "yang123";
 8             ////3 提交
 9             testdb.SaveChanges();                
10             //4. 提交
11              testdb.SaveChanges();
12         }   
13 
14 
15          public void Modify2()
16         {
17             //1.new对象
18             Test1 test1 = new Test1() { Id = 11, Name = "yang1" };
19 
20             #region 修改部分字段
21             //2.附加对象 
22             testdb.Tests.Attach(test1);
23             //3.修改部分字段
24             testdb.Entry(test1).Property("Name").IsModified = true; 
25             #endregion
26 
27             #region 修改全部字段
28             //2.修改全部字段
29             testdb.Entry(test1).State = System.Data.EntityState.Modified; 
30             #endregion
31 
32             //4. 提交
33             testdb.SaveChanges();
34         }
复制代码

Delete

复制代码
 1     public void Delete()
 2         {
 3             //1.先查
 4             Test1 test1 = testdb.Tests.Where(u => u.Name == "yang").FirstOrDefault();
 5             //2.再删除
 6             testdb.Tests.Remove(test1);
 7             //3.提交
 8             testdb.SaveChanges();        
 9         }
10 
11 
12      public void Delete2()
13         {          
14 
15             //1. new对象
16             Test1 test1 = new Test1() { Name = "yang" };
17             //2.附加对象 
18             testdb.Tests.Attach(test1);
19             //3.删除对象
20             testdb.Tests.Remove(test1);
21             //4. 提交
22             testdb.SaveChanges();
23         }
复制代码

Select

复制代码
 1      public void Select()
 2         {
 3             List<Test1> list;
 4             List<Test1> list1;
 5          
 6             
 7             //开始没有查询 ToList后才开始查询
 8             list = testdb.Tests.Where(u => u.Name == "yang").ToList();
 9             list1 = (from u in testdb.Tests where u.Name == "yang" select u).ToList();                                
10 
11         }
复制代码

在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同。

例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外键GenreId连接流派表Genre(GenreId,Name)。每个唱片归属唯一一个流派,一个流派可以对应多个唱片。

1.Join(),两表不必含有外键关系,需要代码手动指定连接外键相等(具有可拓展性,除了值相等,还能指定是>,<以及其他对两表的相应键的关系),以及结果字段。

重载方式(是扩展方法,第一个参数带this,代表自身):

1.public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector);

2.public static IQueryable<TResult> Join<TOuter, TInner, TKey, TResult>(this IQueryable<TOuter> outer, IEnumerable<TInner> inner, Expression<Func<TOuter, TKey>> outerKeySelector, Expression<Func<TInner, TKey>> innerKeySelector, Expression<Func<TOuter, TInner, TResult>> resultSelector, IEqualityComparer<TKey> comparer);

那么可以这么写两个表的连接:

var wholeRecord = dc.Album.Join(dc.Genre, a => a.GenreId, g => g.GenreId, (a, g) => new { a.AlbumId,a.Name,g.GenreId,g.Name;

这样就选取除了两表的AlbumId,Name,GenreId,Name。

2.Include(),两表必须含有外键关系,只需要指定键名对应的类属性名即可,不需指定结果字段(即全部映射)。默认搜索某表时,不会顺带查询外键表,直到真正使用时才会再读取数据库查询;若是使用 Include(),则会在读取本表时把指定的外键表信息也读出来。

重载方式:

//位于namespace System.Data.Entity.Infrastructure
public DbQuery<TResult> Include(string path);
//位于namespace System.Data.Entity,务必引入才能找到该方法。否则只看到上个方法
public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path) where T : class;
        public static IQueryable<T> Include<T>(this IQueryable<T> source, string path) where T : class;

可以这么写:

//EF已经生成了Album和Genre的数据库映射模型类以及导航属性
var wholeRecord=dc.Album.Include("Genre");
//或者
//var wholeRecord=dc.Album.Include(a=>Genre);

这样数据库就执行了一个左连接,把Album和Genre的所有字段全部连起来了,并且Include()是立即查询的,像ToList()一样,不会稍后延迟优化后再加载。

这样其实效率很低,因为如果两张表记录很大,那么连接是个费时费资源的事情,建议少用,或者先筛选出需要的结果集再连接。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值