PDA

View Full Version : مشکل در واکشی داده با استفاده از روش GenericRepository



voice.of.silence
یک شنبه 07 بهمن 1397, 14:48 عصر
سلام دوستان:
من در لایه ی Data از GenericRepository و UnitOfWork استفاده کردم.
اما فقط زمانی که می خوام یک ستون خاص رو فراخوانی کنم برنامه دچار خطا می شه.
اگر همه ی مقادر یک جدول رو فراخوانی کنم مشکل نیست و فقط مورد بالا دچار خطا می شه.
مثلا می خوام که فقط PersonsName رو واکشی کنم.
خوشحال می شم اگه منو در حل این مشکل راهنمایی کنید.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq.Expressions;


using Accountent.DataLayer.Services;
using Accountent.DataLayer.DataModel;


namespace Accountent.DataLayer.Services
{
public class GenericRepository<TEntity> where TEntity:class
{
private AccountentEntities _db;
private DbSet<TEntity> _dbset;


public GenericRepository(AccountentEntities db)
{
_db = db;
_dbset = _db.Set<TEntity>();


}
//end of constractor.


public virtual IEnumerable<TEntity> get(Expression<Func<TEntity, bool>> where = null)
{
IQueryable<TEntity> query = _dbset;
if (where != null)
{
query = query.Where(where);


}
//end of if staitment.


return query.ToList();


}
//end of method.


public virtual TEntity GetById(object id)
{
return _dbset.Find(id);


}
//end of getting by ID.


public virtual TEntity CheckExistingObject(object obj)
{
return _dbset.Find(obj);


}
//end of getting customer ID.


public virtual void Insert(TEntity entity)
{
_dbset.Add(entity);


}
//end of inserting.


public virtual void Update(TEntity entity)
{
_dbset.Attach(entity);
_db.Entry(entity).State = EntityState.Modified;


}
//end of updateing.


public virtual void Delete(TEntity entity)
{
if (_db.Entry(entity).State == EntityState.Detached)
{
_dbset.Attach(entity);


}
//end of if staitment.


_dbset.Remove(entity);
}
//end of deleteing.


public virtual void Delete(object id)
{
var entity = GetById(id);
Delete(entity);


}
//end of deleteing object id.


}
//end of GenericRepository Class.


}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using Accountent.DataLayer.DataModel;
using Accountent.DataLayer.Services;


namespace Accountent.DataLayer.Content
{
public class UnitOfWork : IDisposable
{
AccountentEntities db = new AccountentEntities();


private GenericRepository<tbl_persons> _PersonsRepository;
public GenericRepository<tbl_persons> PersonsRepository
{
get
{
if (_PersonsRepository == null)
{
_PersonsRepository = new GenericRepository<tbl_persons>(db);


}
//end of if staitment.


return _PersonsRepository;
}
}
//end of get.


private GenericRepository<tbl_transaction> _TransActionRepository;
public GenericRepository<tbl_transaction> TransActionRepository
{
get
{
if (_TransActionRepository == null)
{
_TransActionRepository = new GenericRepository<tbl_transaction>(db);


}
//end of if staitment.


return _TransActionRepository;
}
}
//end of get.


public void Save()
{
db.SaveChanges();
}
//end of saveing.


public void Dispose()
{
db.Dispose();
}
//end of method.


}
//end of unitofwork.