EF How to use context.Set and context.Entry, which ships with EF4.1 ?

How to use context.Set and context.Entry, which ships with EF4.1 ?

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-application

However, 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 EntityState

using System.Data.Entity;

using System.Linq.Expressions;  // for Expression command

using TasPOMark4.Models;

namespace TasPOMark4.Models

{

    public class GenericRepository<TEntity> where TEntity : class 

    { 

        internal TasEntities context; 

        internal DbSet<TEntity> dbSet; 

        public GenericRepository(TasEntities context) 

        { 

            this.context = context; 

            this.dbSet = context.Set<TEntity>();

        } 

        public IEnumerable<TEntity> Get( 

            Expression<Func<TEntity, bool>> filter = null, 

            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 

            string includeProperties = "") 

        { 

            IQueryable<TEntity> 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,

Graeme

Edited by: user4487499 on 06-May-2011 02:45

Edited by: user4487499 on 06-May-2011 02:46

Edited by: user4487499 on 06-May-2011 03:21
时间: 2024-10-18 18:33:29

EF How to use context.Set and context.Entry, which ships with EF4.1 ?的相关文章

Linux Context , Interrupts 和 Context Switching 说明【转】

转自:http://blog.csdn.net/tianlesoftware/article/details/6461207 一. 进程Context 定义 当一个进程在执行时, CPU的所有寄存器中的值.进程的状态以及堆栈中的内容,比如各个变量和数据,包括所有的寄存器变量.进程打开的文件.内存信息等.这些信息被称为该进程的上下文(Context). 一个进程的Context可以分为三个部分:用户级上下文.寄存器上下文以及系统级上下文: (1)用户级上下文: 正文.数据.用户堆栈以及共享存储区:

[转载]Difference between &lt;context:annotation-config&gt; vs &lt;context:component-scan&gt;

在国外看到详细的说明一篇,非常浅显透彻.转给国内的筒子们:-) 原文标题: Spring中的<context:annotation-config>与<context:component-scan>到底有什么不同? 原文出处:http://stackoverflow.com/a/7456501 <context:annotation-config> is used to activate annotations in beans already registered in

Spring &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;区别

原版: https://stackoverflow.com/questions/7414794/difference-between-contextannotation-config-vs-contextcomponent-scan/ 翻译版: http://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> is used to activate annotations in beans already re

Spring基础系列16 -- &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;的区别

<context:annotation-config> 和 <context:component-scan>的区别 转载:http://www.cnblogs.com/leiOOlei/p/3713989.html <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解. <context:component-scan>除了

使用&lt;context:component-scan base-package=&quot;&quot; &gt;&lt;/context:component-scan&gt;报500错误

今天练习SSH小程序,在springmvc.xml中使用<context:component-scan base-package="" ></context:component-scan> 组件扫描controller的时候,一直报500错误,在网上查了很久没有解决.错误如下 于是便将其注释掉,使用<bean class=""></bean>使用bean来一个个添加..500错误没有了,但是来了个空指针异常.我打印输出

Spring 开启Annotation &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;诠释及区别

Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的

Spring &lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;诠释及区别

<context:annotation-config> 和 <context:component-scan>的区别 <context:annotation-config> 是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解. <context:component-scan>除了具有<context:annotation-config>的功能之外,<conte

&lt;context:annotation-config /&gt;和 &lt;context:component-scan

<context:annotation-config />和 <context:component-scan>同时存在的时候,前者会被忽略. 也就是那些@autowire,@resource等注入注解只会被注入一次 哪怕是你手动的注册了多个处理器,spring仍然只会处理一次: <context:annotation-config />仅能够在已经在已经注册过的bean上面起作用. 对于没有在spring容器中注册的bean,它并不能执行任何操作. <contex

&lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;的区别

转自:GOOD spring <context:annotation-config> 跟 <context:component-scan>诠释及区别 <context:annotation-config>  是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解,是一个注解处理工具,也就是只管注入,不管注册bean. <context:component-scan> 除了具有&l