简体   繁体   中英

deleting record from oracle database using entity framework

I would like to delete a record using the entity framework. DB is oracle.

Approach 1:

public void DeleteTask(Guid taskId, string userId)
{
    var task = _context.TWFITSKs.FirstOrDefault(x => x.ID == taskId.ToString());//<---Error line
    if (task == null) return;
    _context.TWFITSKs.Attach(task);
    _context.TWFITSKs.Remove(task);
    _context.SaveChanges();
}

Error : ORA-00932: inconsistent datatypes: expected - got CLOB

TWFITSK does contain a column with datatype as CLOB , but not sure why that is causing a problem in this select statement.

Approach 2:

public void DeleteTask(Guid taskId, string userId)
{
    var task = new TWFITSK { ID = taskId.ToString() };
    _context.TWFITSKs.Attach(task); // <--- Error line
    _context.TWFITSKs.Remove(task);
    _context.SaveChanges();
}

Error: System.InvalidOperationException: 'Attaching an entity of type 'XXXXX.TWFITSK' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.'

Approach 3:

public void DeleteTask(Guid taskId, string userId)
{
    var task = new TWFITSK { ID = taskId.ToString() };
    _context.TWFITSKs.Remove(task); //<--- Error line
    _context.SaveChanges();
}

Error: The object cannot be deleted because it was not found in the ObjectStateManager

Why don't you just call .Remove

var task = new TWFITSK { ID = taskId.ToString() };
_context.TWFITSKs.Entry(task).State = EntityState.Deleted;
_context.SaveChanges();

but this may still won't work if you have datatype mismatch. It may be better If you can share table DDL script , class definition and OnModelCreating

You could try changing the entity state to deleted:

var task = new TWFITSK { ID = taskId.ToString() };
_context.Entry(task).State = EntityState.Deleted;
_context.SaveChanges();

Update : try passing the entity to your method as it sounds like its already attached to the context. This may work:

public void DeleteTask(TWFITSKs task)
{
    if (task == null) return;

    _context.TWFITSKs.Remove(task);
    _context.SaveChanges();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM