本文介绍了如何使用包含存储库模式的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hello gyes, i在我的存储库模式中存在一些问题。 i想要获取包括关系表在内的数据... 怎么办,我不知道.. 请帮我修改我的存储库模式 请... // 上下文类 public class InstituteContext:DbContext { public InstituteContext(): base ( InstituteContext) { Database.SetInitializer( new DropCreateDatabaseIfModelChanges( )); } 受保护 覆盖 void OnModelCreating(DbModelBuilder modelBuilder) { base .OnModelCreating(modelBuilder); // 地图学生表 modelBuilder.Entity() .ToTable( tblstudent)。属性(t =& gt; t.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) .HasColumnName( studentid); // 地图地址表 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)。属性(t =& gt; t.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) .HasColumnName( addressid); } } // 学生班级 public class StudentBasicDetail:BaseEntity { [列( fullname)] public string FullName { get ; set ; } [列( dob)] public DateTime? DateOfBirth { get ; set ; } [列( 性别)] public string 性别{ get ; set ; } public virtual ICollection AddressDetails {获得; set ; } } // 地址类 public class AddressDetail:BaseEntity { [列( studentid)] public Int64 StudentId { get ; set ; } [列( 地址)] public string 地址{ get ; set ; } [列( addresstype)] public AddressType AddressType { get ; set ; } public virtual StudentBasicDetail StudentBasicDetail {获得; set ; } } // BaseEntity Class public abstract class BaseEntity { [Key] public Int64 Id {获得; set ; } [列( addeddate)] public DateTime? AddedDate { get ; set ; } [列( 由添加]] public string AddedBy { get ; set ; } [列( modifieddate)] public DateTime? ModifiedDate { get ; set ; } [Column( modifiedby)] public string ModifiedBy { get ; set ; } } // 存储库类 internal interface IRepository:IDisposable 其中 T: class { void 添加(T实体); void 更新(T实体); void 删除( int id); int 保存(); // IQueryable GetAll(表达式< func>& lt; t ,& gt;& gt; [] include); T FindById( int id); } public abstract class 存储库:IRepository 其中 T: class { private InstituteContext _context; protected 存储库(InstituteContext context) { _context = context; } public void Dispose() { if (_context == null )返回; _context.Dispose(); _context = null ; } public virtual 无效添加(T t) { 如果(t!= null ) _context.Set()。Add(t); } public virtual 无效更新(T t) { 如果(t!= null ) _context.Entry(t).State = EntityState.Modified; } public virtual void 删除( 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> :存储库< t> 其中 T: class { public InstituteContext Context { get ; private set ; } public InstituteRepository(InstituteContext context): base (context) { if (context == null ) { throw new ArgumentNullException( 上下文中,); } 上下文=上下文; } } 谢谢&问候 Manamohan 我尝试过: 我正在尝试... 使用(var repo = new InstituteRepository< studentbasicdetail>(new InstituteContext())) { var srch = repo.GetAll(); //这里没有显示像包含表AddressDetail } 解决方案 使用实体框架加载相关实体 - 初学者入门 [ ^ ] 实体框架加载相关实体 [ ^ ] 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 patternplease...//Context Classpublic 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 => i.AddedBy).Ignore(i => i.AddedDate).Ignore(i => i.ModifiedBy).Ignore(i => i.ModifiedDate) .HasRequired(t=>t.StudentBasicDetail) .WithMany(a=>a.AddressDetails) .HasForeignKey(k=>k.StudentId) .WillCascadeOnDelete(false) .ToTable("tbladdress").Property(t => t.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity) .HasColumnName("addressid"); } }// Student Classpublic 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 Classpublic 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 Classpublic 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 Classinternal interface IRepository : IDisposable where T : class { void Add(T entity); void Update(T entity); void Delete(int id); int Save(); // IQueryable GetAll(Expression<func><t,>>[] 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>[] includeExpressions) { var query = _context.Set().AsQueryable(); return includeExpressions.Aggregate(query, (current, include) => 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 & RegardsManamohanWhat 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" } 解决方案 Loading Related Entities with Entity Framework - A Beginner's Primer[^]Entity Framework Loading Related Entities[^] 这篇关于如何使用包含存储库模式的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 03:32