繁体   English   中英

如何使用存储库模式访问EF中的导航属性

[英]How to access navigation properties in EF using repository pattern

上下文:ASP.NET Web应用程序

基本存储库(抽象类)实现了所有其他存储库都将从其继承的必需的基本功能:即-PersonRepo -AddressRepo

编辑:您将查询几个数据库表的代码放在何处,而其中一些与导航属性没有直接关系,分别位于相应的存储库中?

现在,访问导航属性的最佳方法是什么? 使用存储库模式时非常麻烦。 如果没有方便的解决方案,我将保留所有内部内容并保留旧的DAL层,然后直接使用DbContext。

我只是想出一个临时解决方案来访问我的PersonRepository实现中的导航属性:

public Person InsertNewPersonWithAddress(Person p, Address addr)
        {
            this.Insert(p); // insert person
            var ar = new AddressRepository(this.ctx);
            ar.Insert(addr);  // insert address
            this.Addresses(p).Add(addr);  // add address to person

            return p;
        }

        /* Navigation Properties */ 
        ICollection<Address> Addresses(Person p)
        {
            return ctx.Entry<Person>(p).Collection<Address>("Addresses").CurrentValue;
        }

基本存储库类实现以下接口

public interface IRepository<T> where T : class
    {
        IQueryable<T> All();
        IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
        T Insert(T entity);
        IEnumerable<T> InsertMultiple(IEnumerable<T> items);
        T Delete(T entity);
        IEnumerable<T> DeleteMultiple(IEnumerable<T> items);
        T Update(T entity);
        IEnumerable<T> UpdateMultiple(IEnumerable<T> items);
        void Save();
    }

DB-设计:

人员->人员地址(连接表)<-地址

任何智能解决此问题的方法?

您在想这是错误的方式。 Navigation属性是Entity的属性,而不是Repository的属性。 您无需为导航属性实例化存储库,可以将其直接添加到实体。

public Person InsertNewPersonWithAddress(Person p, Address addr)
{
    p.Address = addr;
    this.Insert(p); // insert person

    return p;
}

现在,如果要插入新的Person但希望将其附加到现有Address (而不是插入两者的新副本),则可以将AddressId FK公开为Person Entity上的属性,并传递FK值来执行链接。 无论哪种情况,此存储库都不需要访问另一个存储库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM