-1
DETAILS
I am using . NET Core 3.1 and Mysql 8.0
I’m starting a new project using a Mysql database on Digital Ocean.
I’m using the DDD, I used as a basis to make the structure this website.
I created a ObterTodos()
to test with the database in Digital Ocean.
PROBLEM
While trying to give a ObterTodos()
in the Swagger
, I get this error: Calling ShapedQueryExpression.VisitChildren is not allowed. Visit expression manually for relevant part.
I did a test, where I created a local SQL Server database, ran the create table, adapted the project to receive the SQL Server connection string (Usesqlserver(...)) and worked... Obtentodos was no mistake!
IMAGERY
This is the error in Visual Studio:
This is the mistake in Swagger:
CODE
Controller:
[HttpGet("ObterTodos/")]
public IActionResult Get()
{
try
{
return new ObjectResult(service.Get());
}
catch (Exception ex)
{
return BadRequest(ex);
}
}
Service:
public IList<T> Get() => repository.ObterTodos();
Repository:
protected Contexto context;
protected DbSet<T> DbSet;
public BaseRepository()
{
context = new Contexto();
DbSet = context.Set<T>();
}
public IList<T> ObterTodos()
{
return DbSet.ToList();
}
Context:
public class Contexto : DbContext
{
public static readonly ILoggerFactory MyLoggerFactory = LoggerFactory.Create(builder =>
{
builder
.AddFilter((category, level) =>
category == DbLoggerCategory.Database.Command.Name
&& level == LogLevel.Information)
.AddConsole();
});
public DbSet<Usuario> Usuario { get; set; }
public Contexto() : base() { }
public Contexto(DbContextOptions<Contexto> options) : base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseLoggerFactory(MyLoggerFactory)
.UseMySql("Server=MINHACONEXAO.ondigitalocean.com;Port=PORTADIGITALOCEAN;Database=AppDb;Uid=leonardo;Pwd=MINHASENA");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Usuario>(new UsuarioMap().Configure);
}
}
Model:
public class Usuario : BaseEntity
{
public string Senha { get; set; }
public long? PessoaId { get; set; }
public string PerfilAcesso { get; set; }
}
Baseentity:
public abstract class BaseEntity
{
public long Id { get; set; }
public DateTime? DataCadastro { get; set; }
public DateTime? DataAlterado { get; set; }
public DateTime? DataInativado { get; set; }
public long? UsuarioCadastroId { get; set; }
public long? UsuarioAlteradoId { get; set; }
public long? UsuarioInativadoId { get; set; }
public string Exclusao { get; set; }
}
Map:
public class UsuarioMap : IEntityTypeConfiguration<Usuario>
{
public void Configure(EntityTypeBuilder<Usuario> builder)
{
builder.ToTable("Usuario");
builder.HasKey(c => c.Id);
builder.Property(c => c.Senha)
.IsRequired()
.HasColumnName("Senha")
.HasMaxLength(100);
builder.Property(c => c.PessoaId)
.HasColumnName("PessoaId");
builder.Property(c => c.PerfilAcesso)
.IsRequired()
.HasColumnName("PerfilAcesso")
.HasMaxLength(20);
builder.Property(c => c.DataCadastro)
.HasColumnName("DataCadastro");
builder.Property(c => c.DataAlterado)
.HasColumnName("DataAlterado");
builder.Property(c => c.DataInativado)
.HasColumnName("DataInativado");
builder.Property(c => c.UsuarioCadastroId)
.HasColumnName("UsuarioCadastroId");
builder.Property(c => c.UsuarioAlteradoId)
.HasColumnName("UsuarioAlteradoId");
builder.Property(c => c.UsuarioInativadoId)
.HasColumnName("UsuarioInativadoId");
builder.Property(c => c.Exclusao)
.HasColumnName("Exclusao")
.IsRequired()
.HasMaxLength(1);
}
}
Mysql Query (Create Table Usuario)
CREATE TABLE Usuario(
Id bigint auto_increment primary key,
Senha VARCHAR(100) not null,
PessoaId bigint,
PerfilAcesso varchar(20) not null,
DataCadastro DateTime,
DataAlterado DateTime,
DataInativado DateTime,
UsuarioCadastroId bigint,
UsuarioAlteradoId bigint,
UsuarioInativadoId bigint,
Exclusao CHAR(1) not null,
FOREIGN KEY (UsuarioCadastroId)
REFERENCES Usuario(Id),
FOREIGN KEY (UsuarioAlteradoId)
REFERENCES Usuario(Id),
FOREIGN KEY (UsuarioInativadoId)
REFERENCES Usuario(Id)
);
On which line of the code the error occurs?
– Leandro Angelo
@Leandroangelo on this one
public IList<T> Get() => repository.ObterTodos();
– LeoHenrique