[英]deleting record from oracle database using entity framework
I would like to delete a record using the entity framework. 我想使用实体框架删除一条记录。 DB is oracle.
DB是oracle。
Approach 1: 方法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
错误:ORA-00932:不一致的数据类型:预期-获得CLOB
TWFITSK
does contain a column with datatype as CLOB
, but not sure why that is causing a problem in this select statement. TWFITSK
确实包含一个数据类型为CLOB
的列,但不确定为什么这会导致此select语句出现问题。
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.
错误:System.InvalidOperationException:'附加类型为'XXXXX.TWFITSK'的实体失败,因为相同类型的另一个实体已经具有相同的主键值。 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: 方法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
错误:无法删除该对象,因为在ObjectStateManager中找不到该对象
Why don't you just call .Remove
为什么不打电话给我呢?
.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
如果可以共享表DDL脚本 , 类定义和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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.