I'm trying to use the entity framework in Visual Studio, but when I try to run the program it generates the error:
System.Data.SqlClient.SqlException was unhandled Class=20 ErrorCode=-2146232060 HResult=-2146232060 LineNumber=0 Message=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: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Não é possível criar uma instância automática. Consulte o log de eventos do Aplicativo do Windows para obter detalhes do erro. ) Number=-1983577832 Server="" Source=.Net SqlClient Data Provider State=0 StackTrace: at CursoEntity.Program.Main(String[] args) in c:\users\almeijun\documents\visual studio 2015\Projects\CursoEntity\CursoEntity\Program.cs:line 29 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
The configuration of the App.config file for the connection looks like this:
<connectionStrings>
<add name="EntidadesContext"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\almeijun\documents\visual studio 2015\Projects\CursoEntity\CursoEntity\Loja.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Created classes are:
public class Usuario
{
public int ID { get; set; }
public string Nome { get; set; }
public string Senha { get; set; }
}
public class EntidadesContext : DbContext
{
public DbSet<Usuario> Usuarios { get; set; }
}
class Program
{
static void Main(string[] args)
{
try
{
EntidadesContext contexto = new EntidadesContext();
contexto.Database.CreateIfNotExists();
Usuario usuario = new Usuario() { Nome = "Junior", Senha = "1234" };
contexto.Usuarios.Add(usuario);
contexto.SaveChanges();
contexto.Dispose();
}
catch (Exception ex)
{
throw ex;
}
}
}
Thanks for your attention.