Click here to Skip to main content
15,949,686 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
please...

C#
//Context Class

public class InstituteContext : DbContext
    {
        public InstituteContext()
            : base("InstituteContext")
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges());
        }
        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
           //Map Student Table
            modelBuilder.Entity()
               .ToTable("tblstudent").Property(t => t.Id)
               .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
               .HasColumnName("studentid");
            //Map Address Table
           modelBuilder.Entity<addressdetail>()
               .Ignore(i =&gt; i.AddedBy).Ignore(i =&gt; i.AddedDate).Ignore(i =&gt;                 i.ModifiedBy).Ignore(i =&gt; i.ModifiedDate)
               .HasRequired(t=&gt;t.StudentBasicDetail)
               .WithMany(a=&gt;a.AddressDetails)
               .HasForeignKey(k=&gt;k.StudentId)
               .WillCascadeOnDelete(false)
               .ToTable("tbladdress").Property(t =&gt; t.Id)               
               .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
               .HasColumnName("addressid");
        }
    }

// Student Class

public class StudentBasicDetail : BaseEntity
    {        
        [Column("fullname")]
        public string FullName { get; set; }
            
        [Column("dob")]
        public DateTime? DateOfBirth { get; set; }

        [Column("gender")]
        public string Gender { get; set; }
       
        public virtual ICollection AddressDetails { get; set; }
    }


//Address Class

public class AddressDetail : BaseEntity
    {
        [Column("studentid")]
        public Int64 StudentId { get; set; }
        
        [Column("address")]
        public string Address { get; set; }        

        [Column("addresstype")]
        public AddressType AddressType { get; set; }

        public virtual StudentBasicDetail StudentBasicDetail { get; set; }
    }

// BaseEntity Class

public abstract class BaseEntity
    {
        [Key]
        public Int64 Id { get; set; }

        [Column("addeddate")]
        public DateTime? AddedDate { get; set; }

        [Column("addedby")]
        public string AddedBy { get; set; }

        [Column("modifieddate")]
        public DateTime? ModifiedDate { get; set; }

        [Column("modifiedby")]
        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.Dispose();
            _context = null;
        } 

        public virtual void Add(T t)
        {
           if (t != null)
               _context.Set().Add(t);
        }       

        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);
            _context.Set().Remove(obj);
        }        

        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
Manamohan

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"
}
Posted
Updated 20-Jul-16 22:44pm
v2

1 solution

 
Share this answer
 
Comments
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