Microsoft.EntityFrameworkCore [10100]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Microsoft.EntityFrameworkCore [10100]

Post by Anonymous »

Ich habe dieses Formular und wenn ich versuche, das Formular einzureichen, erhält mein DB ein Register mit Nullfeldern, jedes Feld, das das Formular ausfüllt, das ich Null gesendet habe, aber die Felder, die ich voreinstehe, erreicht der Wert die DB.

Code: Select all






[b]No.[/b]
[b]Cliente[/b]
[b]Asunto[/b]
[b]Resumen del reporte[/b]
[b]Describa con más detalle el registro[/b]
[b]Fecha[/b]
[b]Estado[/b]


@ContadorId


Seleccione una opcion
@foreach (var cliente in Clientes)
{
@cliente.Nombre
}




Seleccione una opcion
@foreach (var asunto in Asuntos)
{
@asunto.Asuntos
}









@Registros.Fecha.ToString("dd/MM/yyyy")


Nuevo




ACEPTAR


Und mein CS ist
@code {
private Registros Registros = new Registros
{
Nombre = string.Empty,
Asunto = string.Empty,
Cliente = string.Empty,
DescCorta = string.Empty,
Descripcion = string.Empty
};
private string? UserName;
private string? UserLastName;
private int ContadorId;
private List Clientes = new List();
private List Asuntos = new List();
private List UserRegistros = new List();
private ApplicationUser? currentUser;

protected override async Task OnInitializedAsync()
{
try
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var userPrincipal = authState.User;

if (userPrincipal.Identity?.IsAuthenticated == true)
{
currentUser = await usermanager.GetUserAsync(userPrincipal);
UserName = currentUser?.FirstName ?? userPrincipal.Identity.Name;
UserLastName = currentUser?.LastName;
}

Registros.Fecha = DateTime.Now;
ContadorId = IdDisplayContador();

// Crear instancias separadas del DbContext de forma correcta
await using var dbContext = DbContextFactory.CreateDbContext();

Clientes = await dbContext.Clientes.ToListAsync();
Asuntos = await dbContext.Asuntos.ToListAsync();
UserRegistros = await dbContext.Registros
.Where(r => r.Nombre == UserName + " " + UserLastName)
.ToListAsync();
}

catch (Exception ex)
{
// Handle exceptions (e.g., log the error)
Console.Error.WriteLine($"Error during initialization: {ex.Message}");
}
}

private async Task LoadClientesAsync()
{
try
{
await using var dbContext = DbContextFactory.CreateDbContext();
Clientes = await dbContext.Clientes.ToListAsync();
}
catch (Exception ex)
{
// Handle exceptions (e.g., log the error)
Console.Error.WriteLine($"Error loading clients: {ex.Message}");
}
}

private async Task LoadAsuntosAsync()
{
try
{
await using var dbContext = DbContextFactory.CreateDbContext();
Asuntos = await dbContext.Asuntos.ToListAsync();
}
catch (Exception ex)
{
// Handle exceptions (e.g., log the error)
Console.Error.WriteLine($"Error loading issues: {ex.Message}");
}
}

private async Task HandleValidSubmit()
{
try
{
// Asegurarse de que UserName y UserLastName estén asignados
if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(UserLastName))
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var userPrincipal = authState.User;

if (userPrincipal.Identity?.IsAuthenticated == true)
{
currentUser = await usermanager.GetUserAsync(userPrincipal);
UserName = currentUser?.FirstName ?? userPrincipal.Identity.Name;
UserLastName = currentUser?.LastName;
}
}

//debe insertar los datos que se llenaron en el formulario
Registros.Nombre = UserName + " " + UserLastName;
Registros.Estado = "Nuevo";

await using (var dbContext = DbContextFactory.CreateDbContext())
{
dbContext.Registros.Add(Registros);
await dbContext.SaveChangesAsync();
}

// Usar un nuevo contexto para recargar los registros
await using (var dbContext = DbContextFactory.CreateDbContext())
{
UserRegistros = await dbContext.Registros
.Where(r => r.Nombre == $"{UserName} {UserLastName}")
.ToListAsync();
}
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error saving record: {ex.Message}");
}
}

private int IdDisplayContador()
{
return ++ContadorId;
}
}
< /code>
Die Konsole Senden Sie mir diese Ausnahme: < /strong> < /p>
fehler: microsoft.EntityFrameworkcore.Query [10100]
Es trat eine Ausnahme beim Iterieren der Ergebnisse eines Abfragers auf. /> system.invalidoperationException: Eine zweite Operation wurde in dieser Kontextinstanz gestartet, bevor eine frühere Operation abgeschlossen wurde. Dies wird normalerweise durch verschiedene Threads gleichzeitig mit derselben Instanz von dbContext verursacht. Weitere Informationen zum Vermeiden von Threading -Problemen mit DBContext finden Sie unter https://go.microsoft.com/fwlink/?linkid=2097913.
unter microsoft.entityFrameworkcore.infrastructure.internal.ConcurrencyDetector Microsoft.EntityFrameworkCore.Query.internal.singlequeryingenumerable1.AsyncEnumerator.MoveNextAsync() System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913. at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection() at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable< /code> 1.aSyncenumerator.MoveXtaSync ()
Fehler während der Initialisierung: Eine zweite Operation wurde in dieser Kontextinstanz gestartet, bevor eine frühere Operation abgeschlossen wurde. Dies wird normalerweise durch verschiedene Threads gleichzeitig mit derselben Instanz von dbContext verursacht. Weitere Informationen zum Vermeiden von Threading -Problemen mit DBContext finden Sie unter https://go.microsoft.com/fwlink/?linkid=2097913.
Ich habe versucht, verschiedene Instanzen zu machen, aber ich bin neu auf Blazor und ich weiß nicht, ob ich es richtig mache. Ich habe bereits die Tabelle überprüft und das Modell und alles sieht gut aus.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post