gpt4 book ai didi

c# - 如何访问通用存储库类中的 DbContext.Entry 方法 (TEntity)?

转载 作者:行者123 更新时间:2023-12-04 16:54:43 29 4
gpt4 key购买 nike

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

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);

我在 Entry 下看到一个红色的波浪线,错误显示

IBusinessEntityContext does not contain a definition for 'Entry' and no extension method 'Entry' accepting a first argument of type IBusinessEntityContext could be found (are you missing a using directive or an assembly reference?)



这是我的 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
}
}

Here is the documentation for 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);

但我不确定这是否正确。

最佳答案

问题是在你的 Update 方法中,编译器只知道 _context类型为 IBusinessEntityContext并且编译器不知道 _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;
}

关于c# - 如何访问通用存储库类中的 DbContext.Entry<TEntity> 方法 (TEntity)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30791654/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com