博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EF How to use context.Set and context.Entry, which ships with EF4.1 ?
阅读量:6552 次
发布时间:2019-06-24

本文共 2662 字,大约阅读时间需要 8 分钟。

 

Hello,I am trying to implement a generic repository as explained on the following link :-http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-applicationHowever, I do not have options for context.Set or context.Entry, which Ships with EF 4.1 - is there some other way of doing it ? Please see problem code in bold below:-using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data; // for EntityStateusing System.Data.Entity;using System.Linq.Expressions;  // for Expression commandusing TasPOMark4.Models;namespace TasPOMark4.Models{    public class GenericRepository
where TEntity : class { internal TasEntities context; internal DbSet
dbSet; public GenericRepository(TasEntities context) { this.context = context; this.dbSet = context.Set
(); } public IEnumerable
Get( Expression
> filter = null, Func
, IOrderedQueryable
> orderBy = null, string includeProperties = "") { IQueryable
query = dbSet; if (filter != null) { query = query.Where(filter); } foreach (var includeProperty in includeProperties.Split (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) { query = query.Include(includeProperty); } if (orderBy != null) { return orderBy(query).ToList(); } else { return query.ToList(); } } public virtual TEntity GetByID(object id) { return dbSet.Find(id); } public virtual void Insert(TEntity entity) { dbSet.Add(entity); } public virtual void Delete(object id) { TEntity entityToDelete = dbSet.Find(id); Delete(entityToDelete); } public virtual void Delete(TEntity entityToDelete) { *//below Context.Entry does not exist* *//if (context.Entry(entityToDelete).State == EntityState.Detached)* *//if(context.ObjectStateManager.GetObjectStateEntry(entityToDelete) == EntityState.Detached)* *//{* dbSet.Attach(entityToDelete); //} dbSet.Remove(entityToDelete); } public virtual void Update(TEntity entityToUpdate) { dbSet.Attach(entityToUpdate); *//context.Entry does not exist* *//context.Entry(entityToUpdate).State = EntityState.Modified;* // GR skinning a cat (06/05/2011) context.ObjectStateManager.ChangeObjectState(entityToUpdate, EntityState.Modified); } } }As you can see directly above, I have attempted to use context.ObjectStateManager.ChangeObjectState - is this correct ? Many thanks in advance for any help someone can provide,GraemeEdited by: user4487499 on 06-May-2011 02:45Edited by: user4487499 on 06-May-2011 02:46Edited by: user4487499 on 06-May-2011 03:21

 

转载地址:http://oynco.baihongyu.com/

你可能感兴趣的文章
SSDB 数据库的图形界面管理工具发布了!
查看>>
Linux:在中国没有真正的新闻
查看>>
Spring代码分析一:加载与初始化
查看>>
在Linux上配置unixODBC和FreeTDS访问MS SQL Server
查看>>
Windows 7 32 上 selenium 2+sikuli解决swfupload类型上传插件
查看>>
Spring boot学习二
查看>>
android4.1.1 Settings WIFI模块浅析
查看>>
bi business inteligence
查看>>
php 和redis
查看>>
计算机代数系统(free!GPL)Yacas
查看>>
Spring系列之-Spring IOC容器设计:依赖注入设计
查看>>
360安全浏览器中iframe顶部会产生多余空白
查看>>
mysql sql php 参数化查询
查看>>
Thrift0.9.2 安装
查看>>
Maven使用大全
查看>>
linux 下添加一个不能登录的用户
查看>>
四周第三次课(2月28日)
查看>>
除了游戏和医疗,腾讯区块链还准备做什么?
查看>>
彻底认识 PendingIntent
查看>>
深入理解 new 操作符
查看>>