2016-06-12 127 views
0

我有一个问题,使删除行为,以及我不是真的消除我只是改变状态。该对象无法删除,因为它在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添加到我的表中“分配存储过程”

enter image description here

,我的存储过程映射在我的实体框架表中

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; }...... 

请帮助我,会发生什么?

+0

您设置包含所有这些实体那是在你的上下文中。虽然派生了IQueryable ,但并不意味着直接在此Set上调用Remove(我怀疑EF可以将其转换为Retrieve + Delete调用)。尝试在上下文中收集数据,然后将EntityState设置为删除。另外,具有全局上下文的using()语句可能会导致错误(因为在离开块时会调用Dispose())。 – DevilSuichiro

+0

你的代码在哪里调用你的Delete_Empresa SP?我没看到它。另外,你是否首先使用实体​​框架代码? – jpgrassi

+0

请说明如何将sproc映射到删除操作。我认为第二个参数'@ Estatus'在这里不起作用。 –

回答

0

你可以达到你想要的,也避免了需要直接从您的上下文调用SP为他们标记为“排除”之前检索所有实体:

using (_context) 
{ 
    string stpSQL = @"EXEC [dbo].[Delete_Empresa] @CveEmpresaPK, @Estatus"; 

    context.Database.ExecuteSqlCommand(TransactionalBehavior.EnsureTransaction, stpSQL, 
     new SqlParameter("@CveEmpresaPK", entity.CveEmpresaPK), 
     new SqlParameter("@Estatus", entity.Estatus)); 
} 
+0

我正在使用我的表的实体,SP是在我的EF模型中用表作为删除函数进行映射,创建和更新的方式与使用EF和SP的CRUD相同,但我可以创建和更新,但是我无法删除 –

相关问题