Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a Mvc4 application. I have a base class and a repositor class that I was using before. (working on mvc3)

These classes really decreases time that I spent on the project. However, today, I am getting errors.

BaseClass
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;

namespace Panelium.Models
{
    public class BaseMethod<TEntity> where TEntity : class
    {
        public Repository N2model = new Repository();
        public bool Insert(TEntity entity)
        {
            return N2model.Add(entity);
        }
        public void InsertorUpdate(TEntity entity, int Id)
        {
            if (Id > 0)
            {
                N2model.Update(entity);
            }
            else
            {
                N2model.Add(entity);
            }
        }
        public bool Update(TEntity entity)
        {
            return N2model.Update(entity);
        }
        public bool Update(TEntity entity, params string[] kismialanlar)
        {
            return N2model.Attach(entity, kismialanlar);
        }
        public bool Delete(int Id)
        {
            return N2model.Delete<TEntity>(Id);
        }
        public bool Delete(int[] Idler)
        {
            try
            {
                for (int i = 0; i < Idler.Length; i++)
                {
                    N2model.Delete<TEntity>(Idler[i]);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public TEntity Select(int Id)
        {
            var sc = N2model.GetByKey<TEntity>(Id);
            if (sc == null)
            {
                return (TEntity)Activator.CreateInstance(typeof(TEntity));
            }
            return sc;
        }
        public IEnumerable<TEntity> SelectAll()
        {
            return N2model.GetAll<TEntity>();
        }
        public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> criteria)
        {
            return N2model.Find<TEntity>(criteria);
        }
    }
}



Repository Class

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data;
using System.Linq.Expressions;
using System.Data.SqlClient;
using System.Data.Metadata.Edm;
using System.Reflection;

namespace Panelium.Models
{
    public class Repository
    {
        private ObjectContext _objectContext;

        public Repository()
        {
            _objectContext = new PaneliumEntities();
        }

        public int TableType { get; set; }

        public TEntity GetByKey<TEntity>(object keyValue) where TEntity : class
        {
            EntityKey key = GetEntityKey<TEntity>(keyValue);

            object originalItem;
            if (ObjectContext.TryGetObjectByKey(key, out originalItem))
            {
                return (TEntity)originalItem;
            }
            return default(TEntity);
        }

        public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
        {
            var entityName = GetEntityName<TEntity>();
            var q = ObjectContext.CreateQuery<TEntity>(entityName);
            //return ObjectContext.CreateQuery<TEntity>(entityName);
            return q;
        }

        public IQueryable<TEntity> GetQuery<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return GetQuery<TEntity>().Where(predicate);
        }

        public IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
        {
            if (sortOrder == SortOrder.Ascending)
            {
                return GetQuery<TEntity>().OrderBy(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
            }
            return GetQuery<TEntity>().OrderByDescending(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
        }

        public IEnumerable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, string>> orderBy, int pageIndex, int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class
        {
            if (sortOrder == SortOrder.Ascending)
            {
                return GetQuery<TEntity>().Where(predicate).OrderBy(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
            }
            return GetQuery<TEntity>().Where(predicate).OrderByDescending(orderBy).Skip(pageIndex).Take(pageSize).AsEnumerable();
        }


        public TEntity First<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
        {
            return GetQuery<TEntity>().FirstOrDefault(predicate);
        }

        public bool Add<TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                return false;
            }
            else
            {
                try
                {
                    ObjectContext.AddObject(GetEntityName<TEntity>(), entity);
                    SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }

        }

        public bool Attach<TEntity>(TEntity entity, params string[] alanlar) where TEntity : class
        {
            if (entity == null)
            {
                return false;
            }
            else
            {
                try
                {
                    ObjectContext.AttachTo(GetEntityName<TEntity>(), entity);
                    ObjectStateEntry entry = ObjectContext.ObjectStateManager.GetObjectStateEntry(entity);
                    foreach (var item in alanlar)
                    {
                        entry.SetModifiedProperty(item);
                    }
                    SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public void SaveChanges()
        {
            this.ObjectContext.SaveChanges();
        }

        public bool Delete<TEntity>(TEntity entity) where TEntity : class
        {
            if (entity == null)
            {
                return false;
            }
            else
            {
                try
                {
                    ObjectContext.DeleteObject(entity);
                    SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }

        }

        public bool Delete<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            TEntity records = First<TEntity>(criteria);
            return Delete<TEntity>(records);

        }

        public bool Delete<TEntity>(int Id) where TEntity : class
        {
            TEntity records = GetByKey<TEntity>(Id);
            return Delete<TEntity>(records);

        }

        public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
        {
            return GetQuery<TEntity>().AsEnumerable();
        }

        public bool Update<TEntity>(TEntity entity) where TEntity : class
        {
            try
            {
                var fqen = GetEntityName<TEntity>();

                object originalItem;
                EntityKey key = ObjectContext.CreateEntityKey(fqen, entity);
                if (ObjectContext.TryGetObjectByKey(key, out originalItem))
                {
                    ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
                    SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        public IEnumerable<TEntity> Find<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            return GetQuery<TEntity>().Where(criteria);
        }

        public int Count<TEntity>() where TEntity : class
        {
            return GetQuery<TEntity>().Count();
        }

        public int Count<TEntity>(Expression<Func<TEntity, bool>> criteria) where TEntity : class
        {
            return GetQuery<TEntity>().Count(criteria);
        }

        private ObjectContext ObjectContext
        {
            get
            {
                return this._objectContext;
            }
        }

        private string GetEntityName<TEntity>() where TEntity : class
        {
            if (TableType == 0)
            {
                // normal tablolar için
                string entitySetName = _objectContext.MetadataWorkspace
                    .GetEntityContainer(_objectContext.DefaultContainerName, DataSpace.CSpace)
                    .BaseEntitySets.Where(bes => bes.ElementType.Name == typeof(TEntity).Name).First().Name;
                return string.Format("{0}.{1}", _objectContext.DefaultContainerName, entitySetName);
            }
            else
            {
                // store procedureler için
                string entitySetName = _objectContext.MetadataWorkspace
                    .GetEntityContainer(_objectContext.DefaultContainerName, DataSpace.CSpace)
                    .FunctionImports.Where(bes => bes.Name == typeof(TEntity).Name).First().Name;
                return string.Format("{0}.{1}", _objectContext.DefaultContainerName, entitySetName);
            }
        }

        private EntityKey GetEntityKey<TEntity>(object keyValue) where TEntity : class
        {
            var entitySetName = GetEntityName<TEntity>();
            var objectSet = ObjectContext.CreateObjectSet<TEntity>();
            var keyPropertyName = objectSet.EntitySet.ElementType.KeyMembers[0].ToString();
            var entityKey = new EntityKey(entitySetName, new[] { new EntityKeyMember(keyPropertyName, keyValue) });
            return entityKey;
        }
    }
}


The class that is in the model.(KisiBLL.cs )
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Panelium.Models
{
    public class KisiBLL : BaseMethod<kisi>
    {
 //This is enough in order to make crud operatins. 

    }
}


The error is on repository.cs
I am getting;

Error Cannot implicitly convert type 'Panelium.Models.PaneliumEntities' to 'System.Data.Objects.ObjectContext' RootOfTheProject\Panelium\Models\Repository.cs
Posted
Updated 7-Jul-16 23:10pm

In your repository class change this line from

C#
private ObjectContext _objectContext;


to

C#
private PaneliumEntities _objectContext;
 
Share this answer
 
Comments
FoxRoot 3-Oct-13 2:10am    
Thanks for your reponse. But why should I do? Before that project, I was using same repository class with no changes. the type of _objectContext was ObjectContext and I was assigning dataentities same as above. I just jumped from MVC3 to MVC4. and I am using VS12 instead of VS10. So should I change it again? and why should I do this?

Thanks.
Emre.
Govindaraj Rangaraj 3-Oct-13 3:46am    
Can you please check if the auto-generated class "PenuliumEntities" is derived from "ObjectContext"? Are you using EF 4.1 +?

http://blogs.msdn.com/b/cesardelatorre/archive/2011/04/14/entity-framework-4-1-just-released.aspx

The objectContext is replaced by new polished DBContext. It is better to switch to DBContext as all further versions of EF will use this.

Just in case you still want to use ObjectContext please refer this:
http://stackoverflow.com/questions/14245153/entity-framework-where-is-my-object-context
FoxRoot 3-Oct-13 3:48am    
public class kisiBLL: BaseMethod< kisi >
Channge

public Repository()
        {
            _objectContext = new PaneliumEntities();
        }


To
_objectContext = ((IObjectContextAdapter)new PaneliumEntities ()).ObjectContext; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900