Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello gyes,
i have some problem in my repository pattern.

i want to fetch data including relational table...
how to do, i dont know..
please help me and modify my repository pattern

//Context Class

public class InstituteContext : DbContext
        public InstituteContext()
            : base("InstituteContext")
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
           //Map Student Table
               .ToTable("tblstudent").Property(t => t.Id)
            //Map Address Table
               .Ignore(i =&gt; i.AddedBy).Ignore(i =&gt; i.AddedDate).Ignore(i =&gt;                 i.ModifiedBy).Ignore(i =&gt; i.ModifiedDate)
               .ToTable("tbladdress").Property(t =&gt; t.Id)               

// Student Class

public class StudentBasicDetail : BaseEntity
        public string FullName { get; set; }
        public DateTime? DateOfBirth { get; set; }

        public string Gender { get; set; }
        public virtual ICollection AddressDetails { get; set; }

//Address Class

public class AddressDetail : BaseEntity
        public Int64 StudentId { get; set; }
        public string Address { get; set; }        

        public AddressType AddressType { get; set; }

        public virtual StudentBasicDetail StudentBasicDetail { get; set; }

// BaseEntity Class

public abstract class BaseEntity
        public Int64 Id { get; set; }

        public DateTime? AddedDate { get; set; }

        public string AddedBy { get; set; }

        public DateTime? ModifiedDate { get; set; }

        public string ModifiedBy { get; set; }

// Repository Class

internal interface IRepository : IDisposable where T : class
        void Add(T entity);
        void Update(T entity);
        void Delete(int id);
        int Save();
        IQueryable GetAll(Expression<func>&lt;t,&gt;&gt;[] include);
        T FindById(int id);

    public abstract class Repository : IRepository where T : class
        private InstituteContext _context;

        protected Repository(InstituteContext context)
            _context = context;

        public void Dispose()
            if (_context == null) return;
            _context = null;

        public virtual void Add(T t)
           if (t != null)

        public virtual void Update(T t)
            if (t != null)
                _context.Entry(t).State = EntityState.Modified;

        public virtual void Delete(int id)
            var obj = FindById(id);

        public virtual int Save()
            var savecnt = _context.SaveChanges();               
            return savecnt;

        public virtual IQueryable GetAll(params Expression&gt;[] includeExpressions)
            var query = _context.Set().AsQueryable();
            return includeExpressions.Aggregate(query, (current, include) =&gt; current.Include(include));

        public virtual T FindById(int id)
            return _context.Set().Find(id);

 public class InstituteRepository<t> : Repository<t> where T : class
        public InstituteContext Context { get; private set; }
        public InstituteRepository(InstituteContext context)
            : base(context)
            if (context == null)
                throw new ArgumentNullException("context");
            Context = context;

Thanks & Regards

What I have tried:

I am trying...

using (var repo = new InstituteRepository<studentbasicdetail>(new InstituteContext()))
var srch=repo.GetAll();// here nothing show Like include table "AddressDetail"
Updated 20-Jul-16 22:44pm

1 solution

Share this answer
Manamohan Jha 21-Jul-16 4:56am    
Above link not useful for me, bcoz i ma not using this line

public DbSet<addressdetail> AddressDetails { get; set; }

my class directly execute on OnModelCreating().
And i want to do all task through repository pattern...
F-ES Sitecore 21-Jul-16 5:01am    
What you need to do is understand how you load related data via the entities exposed by your context class (or how to enable lazy loading). That's all you need to take from those articles and apply it your own situation.

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