我有一个问题,使删除行为,以及我不是真的消除我只是改变状态。该对象无法删除,因为它在ObjectStateManager中找不到。与实体Framwork和存储过程
我使用一个存储过程来删除或更改状态
ALTER PROCEDURE [dbo].[Delete_Empresa]
@CveEmpresaPK int = NULL,
@Estatus bit = NULL
AS
BEGIN
SET NOCOUNT ON;
if @Estatus = 0
begin
update CatEmpresas
set Estatus = 'False'
where CveEmpresaPK = @CveEmpresaPK
end
else
begin
update CatEmpresas
set Estatus = 'True'
where CveEmpresaPK = @CveEmpresaPK
end
END
我我的SP添加到我的表中“分配存储过程”
,我的存储过程映射在我的实体框架表中
C#中的方法是(类BaseRepository.cs):
public void Detele(TEntity entity)
{
try
{
using (_context)
{
_context.Set<TEntity>().Remove(entity);
_context.SaveChanges();
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
我仅使用两个数据
我的库时呼叫BaseRepository.cs:
public class CatEmpresas_Repositorio : Base.BaseRepository<CatEmpresa>, Interfaces.ICatEmpresas
{
private DBControlCalidadEntities _context = null;
/// <summary>
/// Iniciamos una instancia con una clase generica para Create, Update y Detele
/// </summary>
public CatEmpresas_Repositorio()
: base(new DBControlCalidadEntities())
{
}
and my Entitie:
namespace SistemaControl_Entidades {
public partial class CatEmpresa
{
public CatEmpresa()
{
this.CatPersonals = new HashSet<CatPersonal>();
this.Cve1NivelDepto = new HashSet<Cve1NivelDepto>();
}
---->public int CveEmpresaPK { get; set; }
public string NombreEmpresa { get; set; }
public string RazonSocial { get; set; }
public string RFC { get; set; }
public string TipoEmpresa { get; set; }
public string Direccion { get; set; }
public string Colonia { get; set; }
public string CodigoPostal { get; set; }
public Nullable<int> CveEstadoFK { get; set; }
public Nullable<int> CveMpioFK { get; set; }
----->public Nullable<bool> Estatus { get; set; }......
请帮助我,会发生什么?
您设置包含所有这些实体那是在你的上下文中。虽然派生了IQueryable ,但并不意味着直接在此Set上调用Remove(我怀疑EF可以将其转换为Retrieve + Delete调用)。尝试在上下文中收集数据,然后将EntityState设置为删除。另外,具有全局上下文的using()语句可能会导致错误(因为在离开块时会调用Dispose())。 –
DevilSuichiro
你的代码在哪里调用你的Delete_Empresa SP?我没看到它。另外,你是否首先使用实体框架代码? – jpgrassi
请说明如何将sproc映射到删除操作。我认为第二个参数'@ Estatus'在这里不起作用。 –