如何访问我的通用存储库 class 中的 DbContext.Entry<TEntity> 方法 (TEntity)?
How do I access DbContext.Entry<TEntity> Method (TEntity) in my generic repository class?
这应该很明显,但出于某种原因我无法弄清楚。所以在我的通用存储库 class 中,我有一个更新实体的更新方法:
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 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 class:
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 语句 class:
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;
}
}
这个 class 扩展了 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
是 [=14= 类型] 在其继承树的某处。您可以将此添加到您的界面:
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;
}
这应该很明显,但出于某种原因我无法弄清楚。所以在我的通用存储库 class 中,我有一个更新实体的更新方法:
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 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 class:
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 语句 class:
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;
}
}
这个 class 扩展了 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
是 [=14= 类型] 在其继承树的某处。您可以将此添加到您的界面:
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;
}