我正在使用 Microsoft Contoso 大学示例中使用的通用存储库/工作单元代码:
它工作正常(它保存、检索、更新、删除等),直到数据库级别出现错误。特别是,我尝试插入其中一个字段对于传递给它的文本来说太短了。当然失败了。错误被捕获在 try/catch 块中,然后显示错误消息。问题是,在那之后,即使在表单中输入新信息(这是有效的,与正确的文本大小一样),SaveChanges 方法仍然失败,并出现完全相同的错误(字段 XYZ 必须是字符串或数组类型最大长度为“999”)。它只会停止,直到我停止调试并重新启动 ASP.NET 项目的执行
为清楚起见,这是我拥有的代码:(GenericRepository.cs)
public class GenericRepository<TEntity> where TEntity : class
{
internal DBORAContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(DBORAContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
(UnitOfWork.cs)
public class UnitOfWork : IDisposable
{
private DBORAContext context = new DBORAContext();
private GenericRepository<Activity> activityRepository;
public GenericRepository<Activity> ActivityRepository
{
get
{
if (this.activityRepository == null)
{
this.activityRepository = new GenericRepository<Activity>(context);
}
return activityRepository;
}
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
这是为这个特定对象(Activity.cs)调用存储库的类
public class Activity
{
private static readonly UnitOfWork unitOfWork = new UnitOfWork();
public static POCO.Activity Get(Guid activityId)
{
try
{
var thisActivity = unitOfWork.ActivityRepository.Get(a => a.ACTIVITYID == activityId).FirstOrDefault();
if (null != thisActivity)
{
return thisActivity;
}
return null;
}
catch (Exception ex)
{
new Logger(ex.Message);
return null;
}
}
public static POCO.Activity Add(POCO.Activity activityToInsert)
{
try
{
unitOfWork.ActivityRepository.Insert(activityToInsert);
unitOfWork.Save();
return activityToInsert;
}
catch (Exception ex)
{
//log.Error("Add activity error", ex);
new Logger(ex.Message);
throw;
}
}
public static bool Update(POCO.Activity activityToUpdate)
{
try
{
unitOfWork.ActivityRepository.Update(activityToUpdate);
unitOfWork.Save();
return true;
}
catch (Exception ex)
{
//log.Error("update activity error", ex);
new Logger(ex.Message);
throw;
}
}
public static bool Delete(POCO.Activity activityToDelete)
{
try
{
unitOfWork.ActivityRepository.Delete(activityToDelete);
unitOfWork.Save();
return true;
}
catch (Exception ex)
{
new Logger(ex.Message);
throw;
}
}
}
我在 Oracle 数据库上使用 EF 4.3。请帮忙。