如何在我的通用存储库类中访问 DbContext.Entry 方法 (TEntity)

本文关键字:TEntity DbContext Entry 方法 访问 我的 存储 | 更新日期: 2023-09-27 17:55:39

这应该非常明显,但由于某种原因我无法弄清楚。因此,在我的通用存储库类中,我有一个更新方法来更新实体:

public void Update<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity
{
    try
    {
        if (entity == null)
        {
            throw new ArgumentNullException("entity");
        }
        TEntity existing = _context.Set<TEntity>().Find(entity.Id);
        if (existing != null)
        {
            _context.Entry(existing).CurrentValues.SetValues(entity);
            this._context.SaveChanges();
        }
    }
    catch (DbEntityValidationException dbEx)
    {
        var msg = string.Empty;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                msg += Environment.NewLine + string.Format("Property: {0} Error: {1}",
                validationError.PropertyName, validationError.ErrorMessage);
            }
        }
        var fail = new Exception(msg, dbEx);
        throw fail;
    }
}

问题是我在这一行上收到错误:

_context.Entry(existing).CurrentValues.SetValues(entity);

我在"条目"下得到一个红色波浪线,错误显示

IBusinessEntityContext不包含"Entry"的定义,并且找不到接受IBusinessEntityContext类型第一个参数的扩展方法"Entry"(您是否缺少使用指令或程序集引用?

这是我的IBusinessEntityContext类:

namespace ContentManager.Employees.EF
{
    using System.Data.Entity;
    using Models.Employees;
    public interface IBusinessEntityContext
    {
        DbSet<TEntity> Set<TEntity>() where TEntity : class, IBusinessEntity;
        int SaveChanges();
    }
}

我在存储库类的顶部有一堆 using 语句:

namespace ContentManager.Employees.EF
{
    using System;
    using System.Data.Entity.Validation;
    using System.Linq;
    using System.Reflection;
    using System.Data.Entity;
    using Models.Employees;
    using Models.Employees.EF;
    using System.Data.Entity.Infrastructure;
    public class EFEmployeeEntityRepository : IEFEmployeeEntityRepository
    {
        // bunch of code
    }
}

以下是 DbContext.Entry Method (TEntity) 的文档。它说命名空间只是我正在使用的System.Data.Entity。从上面的代码中可以看出,这个存储库实现了IEFEmployeeEntityRepository。如有必要,其代码如下:

namespace ContentManager.Employees.EF
{
    using System.Linq;
    using Models.Employees.EF;
    public interface IEFEmployeeEntityRepository: IEmployeeEntityRepository
    {
        IQueryable<TEntity> BeginQuery<TEntity>() where TEntity : EFBusinessEntity;
    }
}

这个类扩展了IEmployeeEntityRepository,所以它的代码在这里:

public interface IEmployeeEntityRepository
{
    TEntity GetById<TEntity>(Guid id) where TEntity : class, IBusinessEntity;
    void Insert<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity;
    void Update<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity;
    void Delete<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity;
}

有谁知道为什么我不能使用 Entry 方法?文档说该方法只是

public DbEntityEntry<TEntity> Entry<TEntity>(
    TEntity entity
)
where TEntity : class

所以我想知道我是否可以在自己身上写这个方法?但是我担心该行中的其他方法(即CurrentValues.SetValues(entity))也不起作用。或者,这条线甚至有必要吗?我从中获取代码的教程编写了更新方法,只需

this._context.SaveChanges();

但我觉得这无济于事,所以我添加了

_context.Entry(existing).CurrentValues.SetValues(entity);

但我不确定这是否正确。

如何在我的通用存储库类中访问 DbContext.Entry<TEntity> 方法 (TEntity)

问题是,在 Update 方法中,编译器只知道_contextIBusinessEntityContext 类型,而编译器不知道_context在其继承树中的某处具有类型 DbContext。您可以将其添加到您的界面中:

public interface IBusinessEntityContext
{
    DbSet<TEntity> Set<TEntity>() where TEntity : class, IBusinessEntity;
    int SaveChanges();
    // Add this
    DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class, IBusinessEntity;
}