-2
This is my connection string
"ConnectionStrings": {
"bdcoreconn": "Server=.\\SQLEXPRESS;Database=BdCoreCrud;Trusted_Connection=False;MultipleActiveResultSets=true;User Id=sa;Password=Simb@d123;"
},
In Startup.Cs I have this:
services.AddDbContext<CrudContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("bdcoreconn")));
This is my Context:
public class CrudContext: DbContext
{
public CrudContext(DbContextOptions options) : base(options) { }
public DbSet<Desenvolvedor> Desenvolvedores { get;set; }
public DbSet<Projeto> Projetos { get; set; }
public DbSet<LancamentoHoras> Lancamentos { get; set; }
public DbSet<ProjetoDesenvolvedor> ProjetoDesenvolvedores { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Desenvolvedor>()
.HasKey(p => p.IdDesenvolvedor);
modelBuilder.Entity<LancamentoHoras>()
.HasKey(p => p.IdLancamentoHoras);
modelBuilder.Entity<Projeto>()
.HasKey(p => p.IdProjeto);
modelBuilder.Entity<ProjetoDesenvolvedor>()
.HasKey(p => p.IdProjetoDesenvolvedor);
}
}
When I give one SaveChanges()
, caught that mistake:
Microsoft.Data.Sqlclient.Sqlexception (0x80131904): A network-Related or instance-specific error occurred while establishing a Connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote Connections. (Provider: Named Pipes Provider, error: 40 - Could not open a Connection to SQL Server)
Someone knows how I fix it?
I changed in startup the way to get the connection string for this one
var connection = @"Server=DESKTOP-K4MI90D\\SQLEXPRESS;Initial Catalog=BdCoreCrud;Trusted_Connection=False;MultipleActiveResultSets=true;User Id=sa;Password=Simb@d123;";
services.AddDbContext<CrudContext>(options => options.UseSqlServer(connection));
Note that in Swagger the error has changed. Such a
Error 500 Undocumented
And I don’t know what that is
"Someone knows how I fix it?" is needed for a valid string and confirm that the database is responding with this setting. Already tried to connect with these Settings in Management Studio?
– Ricardo Pontual
@Ricardopunctual, that’s how I connect. That last configuration was attempt. But the connection by Management is that way:
DESKTOP-K4MI90D\SQLEXPRESS
and other user and password. And yet I get the same error. I just haven’t restarted the machine yet.– pnet
these double bars can be a problem, already tried with
.\SQLEXPRESS
?– Ricardo Pontual
It does not accept simple bar. It gives error of face. I tried localhost and the same thing.
– pnet
The basic error is:
Could not open connection to sql server
. Is there any other step I need to take?– pnet
@pnet You have already guaranteed that you have access to the base used in the string Connection, right?
– Jéf Bueno
@LINQ, yes. I use, because the tables in this database I created by management. I think there is a map or whatever.
– pnet
I made an edit with a new way to get the connection string and the error message changed.
– pnet
By all indications your connection string is still wrong or you have some Sqlexpress configuration problem on your machine
– Leandro Angelo
@Leandroangelo, what was wrong was Context. The method was not defined:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
done it worked. I thank everyone there.– pnet