I am trying to use a repository pattern in Entity Framework on Oracle 10g. I simplified my code:
Here is the SQL code:
-- Create table create table TESTTABLE ( MODULE_UNIQUE_ID VARCHAR2(32) not null, PANEL_STATUS VARCHAR2(8) not null ) tablespace SYSTEM pctfree 10 pctused 40 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table TESTTABLE add constraint TESTTABLE_PK_01 unique (MODULE_UNIQUE_ID) using index tablespace SYSTEM pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );
I am creating a simple repository class:
public interface IRepository<TEntity, TCtx> { TCtx Session { get;} void Add(TEntity entity); void AddOrAttach(TEntity entity); void Delete(TEntity entity); int Save(); TEntity SelectByKey(string colName, string key); } public class Repository<TEntity, TCtx> : IRepository<TEntity, TCtx>, IDisposable where TEntity : EntityObject where TCtx : ObjectContext { #region Private fields private TCtx _ctx; private string _keyProperty = "ID"; public int Save() { return _ctx.SaveChanges(); } public TEntity SelectByKey(string colName, string key) { KeyProperty = colName;
The problem is that first I select some object, and then I want to delete. Choosing to work with Oracle DB is good. The problem is updating or deleting the command.
var _repo = new Repository<TESTTABLE, Entities>( new Entities(ConfigurationManager.ConnectionStrings["Entities"] .ConnectionString)); var obj = _repo.SelectByKey("MODULE_UNIQUE_ID", "11111"); _repo.Delete(obj); _repo.Save();
I get this exception:
{"An error occurred while updating records. Exception for details." }
Internal exception:
{"The operation is invalid due to the current state of the object." }
Stacktrace:
in System.Data.Mapping.Update.Internal.UpdateTranslator.CreateCommand (DbModificationCommandTree commandTree) in System.Data.Mapping.Update.Internal.DynamicUpdateCommand.CreateCommand (UpdateTranslator translator, dictionary 2 identifierValues) at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary 2 identifierValues, List`1 generatedValues) in System.Data.Mapping.Update.Internal.UpdateTranslator.Update (IEntityStateManager stateManager, IEntityAdapter adapter)
I tested my repository class again. The MS SQL database works well.
What can cause this problem?