Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityFramework SaveChanges stops working after error
    primarykey
    data
    text
    <p>I am using the Generic Repository/Unit of work code used on the Microsoft Contoso University sample:</p> <p><a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application" rel="nofollow">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application</a></p> <p>And it works fine (it saves, retrieves, updates, delete etc) until there is an error at the database level. In particular, I tried to make an insert in which one of the fields was too short for the text that was passed to it. Of course it failed. The error is trapped in a try/catch block, and then an error message is displayed. Problem is, after that, even when entering new information to the form (this is, valid, as in with the right text size), the SaveChanges method keeps failing, with exactly the same error (field XYZ must be a string or array type with a maximum length of '999'). It only stops until I stop the debug and I restart the execution of the ASP.NET project</p> <p>For clarity purposes, this is the code I have: (GenericRepository.cs)</p> <pre><code>public class GenericRepository&lt;TEntity&gt; where TEntity : class { internal DBORAContext context; internal DbSet&lt;TEntity&gt; dbSet; public GenericRepository(DBORAContext context) { this.context = context; this.dbSet = context.Set&lt;TEntity&gt;(); } public virtual IEnumerable&lt;TEntity&gt; Get( Expression&lt;Func&lt;TEntity, bool&gt;&gt; filter = null, Func&lt;IQueryable&lt;TEntity&gt;, IOrderedQueryable&lt;TEntity&gt;&gt; orderBy = null, string includeProperties = "") { IQueryable&lt;TEntity&gt; 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; } } </code></pre> <p>(UnitOfWork.cs)</p> <pre><code>public class UnitOfWork : IDisposable { private DBORAContext context = new DBORAContext(); private GenericRepository&lt;Activity&gt; activityRepository; public GenericRepository&lt;Activity&gt; ActivityRepository { get { if (this.activityRepository == null) { this.activityRepository = new GenericRepository&lt;Activity&gt;(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); } } </code></pre> <p>And this is the class that calls the repository for this particular object (Activity.cs)</p> <pre><code>public class Activity { private static readonly UnitOfWork unitOfWork = new UnitOfWork(); public static POCO.Activity Get(Guid activityId) { try { var thisActivity = unitOfWork.ActivityRepository.Get(a =&gt; 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; } } } </code></pre> <p>I'm using EF 4.3 on a Oracle database. Please help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload