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

Posted 吴晓阳

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于EF How to use context.Set and context.Entry, which ships with EF4.1 ?的主要内容,如果未能解决你的问题,请参考以下文章

How to use MJRefresh

How to Use SFTP ?

Learn how to use Latex.

How to use GITHUB to do source control

How to use GITHUB to do source control

How To Use the AWK language to Manipulate Text in Linux