با سلام بنده یک کلاس جنریک در لایه DAL نوشتم :
using System; using System.Collections; using System.Collections.Specialized; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Text; using System.ComponentModel; using System.Data.Entity; using System.Reflection; using System.Linq.Expressions; namespace DAL_Vacation { public interface IRepository<T> where T : class { IQueryable<T> GetAll(); IQueryable<T> FindBy(Expression<Func<T, bool>> predicate); void Add(T entity); void Delete(T entity); void Edit(T entity); void Save(); } public class Repository<C, T> : IRepository<T> where T : class where C : DbContext, new() { private C _entities = new C(); public C Context { get { return _entities; } set { _entities = value; } } public virtual IQueryable<T> GetAll() { IQueryable<T> query = _entities.Set<T>(); return query; } public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate) { IQueryable<T> query = _entities.Set<T>().Where(predicate); return query; } public virtual void Add(T entity) { _entities.Set<T>().Add(entity); } public virtual void Delete(T entity) { _entities.Set<T>().Remove(entity); } public virtual void Edit(T entity) { _entities.Entry(entity).State = System.Data.Entity.EntityState.Modified; } public virtual void Save() { _entities.SaveChanges(); } } }
و در لایه BLL خودم می خواهم ذخیره سازی انجام دهم و کدش :
repository = new DAL_Vacation.Repository<DAL_Vacation.AttendanceEntities, Attendance_DomainModel.Mission.TBH_PRSNL_WEB_MISSION>(); DAL_Vacation.Repository<DAL_Vacation.AttendanceEntities, Attendance_DomainModel.Mission.TBCMISSION_TYPE> search = new DAL_Vacation.Repository<DAL_Vacation.AttendanceEntities, Attendance_DomainModel.Mission.TBCMISSION_TYPE>(); Attendance_DomainModel.Mission.TBCMISSION_TYPE newtype = new Attendance_DomainModel.Mission.TBCMISSION_TYPE(); var list = search.FindBy(x => x.CODE == 1).ToList(); if (list.Count == 1) newtype = list[0]; mission.MISSION_TYPE_CODE = newtype.CODE; mission.TBCMISSION_TYPE = newtype; repository.Add(mission); repository.Save();
در لایه PL مدل پاس داده شده را پر کرده ام ولی خطای ذیل هنگام ذخیره سازی نمایش داده می شود :
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
مشکل از کجاست ؟!
کسی راه حلی نداشت؟!؟!؟
فکر نکنم کسی بلد باشه!!!
استادهای گرامی هم که وقت ندارن!
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)