11

Sto tentando di chiamare una stored procedure da EntityFramework che utilizza il parametro Valore tabella.EntityFrameWork e TableValued Parametro

Ma quando provo a fare funzione di importazione Continuo a ricevere un messaggio di avviso dicendo -

La funzione 'InsertPerson' ha un parametro 'InsertPerson_TVP' a indice parametro 0 che ha un tipo di dati 'tipo di tabella 'che è attualmente non supportato per la versione di .NET Framework di destinazione. La funzione era esclusa.

Ho fatto una ricerca iniziale di qui e abbiamo trovato alcuni post che dice che è possibile in EntityFramework con alcuni arounds di lavoro e few dicendo che non è supportato nelle versioni attuali.

Qualcuno conosce un approccio o una soluzione migliore per questo problema?

+1

EF non supporta TVP. Se hai trovato una soluzione alternativa, provalo o non utilizzare EF per chiamare stored procedure con TVP. –

risposta

16

ho finito per fare questo, prega di notare che stiamo lavorando su EF DataContext (non ObjectContext)

Esecuzione di una stored procedure con il parametro di uscita

 using (DataContext context = new DataContext()) 
     {     
      ////Create table value parameter 
      DataTable dt = new DataTable(); 
      dt.Columns.Add("Displayname"); 
      dt.Columns.Add("FirstName"); 
      dt.Columns.Add("LastName"); 
      dt.Columns.Add("TimeStamp"); 

      DataRow dr = dt.NewRow(); 
      dr["Displayname"] = "DisplayName"; 
      dr["FirstName"] = "FirstName"; 
      dr["LastName"] ="LastName"; 
      dr["TimeStamp"] = "TimeStamp";    
      dt.Rows.Add(dr); 

      ////Use DbType.Structured for TVP 
      var userdetails = new SqlParameter("UserDetails", SqlDbType.Structured); 
      userdetails.Value = dt; 
      userdetails.TypeName = "UserType"; 

      ////Parameter for SP output 
      var result = new SqlParameter("ResultList", SqlDbType.NVarChar, 4000); 
      result.Direction = ParameterDirection.Output; 

      context.Database.ExecuteSqlCommand("EXEC UserImport @UserDetails, @ResultList OUTPUT", userdetails, result); 

      return result == null ? string.Empty : result.Value.ToString(); 
     } 

mio Tabella valore di parametro (UDT tabella) copione simile a questa:

CREATE TYPE [dbo].[UserType] AS TABLE (
    [DisplayName]  NVARCHAR (256) NULL, 
    [FirstName]  NVARCHAR (256) NULL, 
    [LastName]   NVARCHAR (256) NULL, 
    [TimeStamp]  DATETIME   NULL 
    ) 

E il mio negozio procedura inizia come

CREATE PROCEDURE UserImport 
-- Add the parameters for the stored procedure here 
@UserDetails UserType Readonly, 
@ResultList NVARCHAR(MAX) output 
AS 

Per stored procedure senza output parametro non abbiamo bisogno di alcun parametro ouput aggiunto/passato a SP.

Spero che ne aiuti qualcuno.

+0

Dov'è l'istanziazione del dr? Ricevo errori dovuti al livello di protezione di DataRowBuilder. – JSideris

+1

@Bizorke Codice modificato. – ssilas777

+0

non ha la funzione UDT (User Defined Type) e non UDF (User Defined Function)? – Dementic

4

forse potremmo anche prendere in considerazione il metodo SqlQuery:

[Invoke] 
public SomeResultType GetResult(List<int> someIdList) 
{ 
    var idTbl = new DataTable(); 
    idTbl.Columns.Add("Some_ID"); 

    someIdList.ForEach(id => idTbl.Rows.Add(id)); 

    var idParam = new SqlParamter("SomeParamName", SqlDbType.Structured); 
    idParam.TypeName = "SomeTBVType"; 
    idParam.Value = idTbl; 

    // Return type will be IEnumerable<T> 
    var result = DbContext.Database.SqlQuery<SomeResultType>("EXEC SomeSPName, @SomeParamName", idParam); 

    // We can enumerate the result... 

    var enu = result.GetEnumerator(); 
    if (!enu.MoveNext()) return null; 
    return enu.Current; 
} 
1
  var detailTbl = new DataTable(); 
     detailTbl.Columns.Add("DetailID"); 
     detailTbl.Columns.Add("Qty"); 
     txnDetails.ForEach(detail => detailTbl.Rows.Add(detail.DetailID, detail.Qty)); 

     var userIdParam = new System.Data.SqlClient.SqlParameter("@UserID", SqlDbType.Int); 
     userIdParam.Value = 1; 

     var detailParam = new System.Data.SqlClient.SqlParameter("@Details", SqlDbType.Structured); 
     detailParam.TypeName = "DetailUpdate"; 
     detailParam.Value = detailTbl; 

     var txnTypeParam = new System.Data.SqlClient.SqlParameter("@TransactionType", SqlDbType.VarChar); 
     txnTypeParam.Value = txnType; 

     var result = await db.Database.ExecuteSqlCommandAsync("MySP @UserID, @Details, @TransactionType", userIdParam, detailParam, txnTypeParam); 
     if(result >= 0) 
      return StatusCode(HttpStatusCode.OK); 
     else 
      return StatusCode(HttpStatusCode.InternalServerError); 
Problemi correlati