Sto usando la funzione CompiledQueries dell'entità framewok core 2.0, ma anche se non fornisce l'errore di sintassi, quando il programma è in esecuzione, ottengo questa eccezione:
Valore "Impossibile analizzare espressione" (Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [TaskManager.EntitiesDB.USER]) AsNoTracking (). Where (__ conditions) ': Gli argomenti specificati non corrispondono agli argomenti previsti: L'oggetto di tipo 'System.Linq.Expressions.TypedParameterExpression' non può essere convertito in tipo 'System.Linq.Expressions.LambdaExpression'. "
Non ho idea di cosa sia. Ho bisogno di aiuto.
Ecco il codice sorgente del mio repository che mostra questo errore:
/// <summary>
/// Query of user
/// </summary>
/// <param name="_context">context</param>
/// <param name="conditions">conditions for clause "Where"</param>
/// <returns>first task that meets the conditions or null </returns>
private static Func<Context, Func<USER, bool>, Task<UserResponseDTO>>
_getUser = EF.CompileAsyncQuery (
(Context _context, Func<USER, bool> conditions) =>
(conditions == null)?
_context.USER.Select(user =>
new UserResponseDTO {
NAME = user.NAME,
EMAIL = user.EMAIL,
SEQUSER = user.SEQUSER,
DATEREGISTER = user.DATEREGISTER
}
).AsNoTracking().FirstOrDefault ()
:
_context.USER.AsNoTracking()
.Where(conditions).Select(user =>
new UserResponseDTO {
NAME = user.NAME,
EMAIL = user.EMAIL,
SEQUSER = user.SEQUSER,
DATEREGISTER = user.DATEREGISTER
}
).FirstOrDefault ()
);
la mia classe che implementa DbContext:
public class Context: DbContext
{
public Context(DbContextOptions<Context> options): base(options){ }
public virtual DbSet<USER> USER { get; set; }
public virtual DbSet<TASK> TASK { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder){
#region Sequences
modelBuilder.HasSequence<long>
("S_SEQUSER").StartsAt(1).HasMin(1);
modelBuilder.HasSequence<long>
("S_SEQTASK").StartsAt(1).HasMin(1);
#endregion
#region propeties Mapping
modelBuilder.Entity<USER>()
.Property(u => u.SEQUSER)
.HasDefaultValueSql(" nextval('\"S_SEQUSER\"') ");
modelBuilder.Entity<TASK>()
.Property(u => u.SEQTASK)
.HasDefaultValueSql(" nextval('\"S_SEQTASK\"') ");
modelBuilder.Entity<USER>()
.HasMany(u => u.TASK)
.WithOne(u => u.USER);
#endregion
base.OnModelCreating(modelBuilder);
}
}
la mia entità di database:
/// <summary>
/// query user
/// </summary>
[Table(@"USER")]
public class USER
{
#region Properties
/// <summary>
/// sequence of user
/// </summary>
/// <value></value>
[Key()]
[Column(@"SEQUSER")]
public virtual long SEQUSER { get; set; }
/// <summary>
/// email of user
/// </summary>
/// <value></value>
[Required]
[StringLength(120)]
[Column(@"EMAIL")]
public virtual string EMAIL { get; set; }
/// <summary>
/// password of user
/// </summary>
/// <value></value>
[Required]
[StringLength(15)]
[Column(@"PASSWORD")]
public virtual string PASSWORD { get; set; }
/// <summary>
/// name of user
/// </summary>
/// <value></value>
[Required]
[StringLength(120)]
[Column(@"NAME")]
public virtual string NAME { get; set; }
/// <summary>
/// date of user was registered
/// </summary>
/// <value></value>
[Column(@"DATEREGISTER")]
public virtual DateTime DATEREGISTER { get; set; }
/// <summary>
/// last acess of user in application
/// </summary>
/// <value></value>
[Column(@"LASTACESS")]
public virtual DateTime LASTACESS { get; set; }
/// <summary>
/// list of task
/// </summary>
/// <value></value>
[InverseProperty("USER")]
public virtual List<TASK> TASK { get; set; }
#endregion
}
Qualcuno potrebbe darmi una mano in questo?
Prova a scomporlo.
Inizia da un semplice caso di prova (come un new UserResponseDTO
fittizio new UserResponseDTO
), quindi prova con una semplice selezione statica (seleziona un utente specifico), quindi aggiungi la condizione if / else e prova con e senza conditions
, quindi aggiungi una semplice selezione statica a il tuo primo ramo if / else, poi fai lo stesso con il secondo ramo, fino a quando non arrivi alla frase che hai ora.
Puoi anche fare una selezione (senza se / else) prima, per verificare se il tuo problema è nella definizione del contesto. Forse in una delle righe che contengono " nextval('\"S_SEQUSER\"') "
o " nextval('\"S_SEQTASK\"') "
. btw: perché gli spazi qui?
Da qualche parte, qualcosa andrà storto. La differenza è: ora sai dove ;-)
E sicuramente scrivere un test unitario per questo. Di solito è solo un clic con il pulsante destro del mouse.