Connect Azure SQL Entity Framework 7 - c#

I'm trying to use a Azure SQL instance and use code first migrations to create the database.
When I use without run migrations it throws an error:
Invalid object name 'MyObject'
And I can see in the exception details my connectionID, because of this I believe that it could handle the connection.
But when I try to use migrations database update to create the object, I can't connect when I use myContext.Database.EnsureCreated(); I can't see the connection id and the exception is
can't connect to the server
I already created a firewall rule to my ip address and it didn't worked.
Could anyone help me?
Thanks in advance.

I figure it out.
The string connection was wrong, i copied from the VS SqlExplorer, i thought it was correct but that wasn't the true.
I see on the manager azure old portal examples for string connection using tcp indicating the port.

Related

EF/MS SQL: A network-related or instance-specific error

There are multiple questions here and in google results regarding this error. But I can't find any situation with the simptoms I have.
So, my code constructs DbContext with connection string buildin from configuration file:
Any query fails with error specified in topic header:
I 100% know that I can connect to the server with such name using credentials specified in connection string:
More, I can communicate with the database using EF infrastructure. For example migrations via VS powershell:
This problem occured after I tried to add LocalDB support on another laptop that has no SQL Server installed.
What I have tried:
obvious SQL Server process/VS/computer restart
fixing connection factory in app.config
reinstalling EF after deleting app.config
hardcoding connection string with SqlConnectionFactory
But still I have no success. What I can do else?
I didn't find root of the problem but was able to fix this issue by removing Automigrating DBContext initializer and updating DB manually.
This is confusing a bit because before I had no problems with automigrations and initializer.

TransactionScope on Azure App Services with Sql Azure

So I have the following trivial code in a WebAPI controller that is published to an Azure App Service website.
using (var tx = new TransactionScope())
{
var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["OrganizationManagement"].ConnectionString);
connection.Open();
return Enumerable.Empty<TimeSessionDTO>();
}
100% of the time this is giving me a transport error exception on the Open call:
A transport-level error has occurred when receiving results from the
server. (provider: TCP Provider, error: 0 - An existing connection was
forcibly closed by the remote host.)
I have tried using ReliablSqlConnection from the Transient Fault Handling EL block using an exponential retry policy, and I just end up with a transaction timeout with that.
If I remove the surrounding TransactionScope, it works and does not throw an exception.
If I run the same code on my local machine with the connection string still pointing to the SQL Azure database, it works fine with the TransactionScope.
What could be going on that I cannot open a database connection inside of a transaction, in an Azure website?
Update: I should also note that using an Entity Framework DbContext inside of a TransactionScope was working fine. It's just choking on plain ADO.NET for some reason.
FYI I also tried it on a new MVC application on Azure, with the same result. I just don't get it :)
Wow, so the problem seems to have been with the connection string. When I first deployed the database I let the database project build the connection string from the server/database/user info, which I later added to the Web.config file in the WebAPI project. Then when I deployed the WebAPI project I guess it saved that connection string in the publish profile.
It turns out that connection string used a little different format and different options than what is supplied when you view the connection strings in the Azure portal. I had already changed it in the Web.config file, but it seems that what is in the publish profile overwrites what is in Web.config, so the change never took effect on the server.
I guess that explains why it worked when I ran it locally, but I have no idea why it only failed when it was in a transaction.

How connect C# to Postgresql in host j.layershift.co.uk

I have installed postgresql database in http://postgres-project-1241043.j.layershift.co.uk/ host.
I want to connect to the database using C#. I use Npgsql with following connection string.
connectionString = # "Server = postgres-project-1241043.j.layershift.co.uk, Port = 5432, User Id = postgre; Password = abcdef; Database = dbluanvantn;";
But I am not able to connect to the server and get error:
Npgsql.NpgsqlException: Failed to a connection to
'postgres-project1241043.j.layershift.co.uk'.
Am I using correct connection string?. Help me fix it.
You can only connect to Postgres (on our Jelastic service) if you add a public IP to the node first. Without that step, you can only connect to it locally (i.e. from another server within your Jelastic environment).
Also I want to mention that you are always welcome to contact our support team (our tech. support is 24x7 and completely free; even for our trial accounts) if you need any further help.

Visual C# 2010: Unable to connect to XAMPP localhost

I am unable to connect to XAMPP localhost in my C# Windows form application.
My code is:
SqlConnection connection = null;
try
{
connection = new SqlConnection("user id=root;" +
"password=12345678;" + "server=localhost");
connection.Open();
}
catch (Exception exptn)
{
MessageBox.Show(exptn.ToString());
}
I made sure to include "using System.Data.SqlClient;" so that my code runs. The timeout error I get at runtime is:
"System.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."
There was more to the error message, but it was uncopyable and too long to post here. I have tried to install MySql and the .NET MySql connector separately from XAMPP. I added MySql.Data as a reference in Solution Explorer (My research told me to add MySql.Data and MySql.Web, and MySql.Data was the only one listed in the options.). Another source told me to add a Microsoft ODBC Data Source in Server/Database explorer, but the option was not available in Database Explorer. I have made sure XAMPP is running and working properly in phpMyAdmin, and I am able to create tables and run queries there. Many sources also seem to have different opinions as to what is needed in the connection string.
I am completely at a loss as to how I am supposed to connect to localhost databases from another application on the same computer. Forgive me if I am overlooking something basic or if this is a noob question, but I know little about connecting to databases (this type of thing is precisely the reason why). Can anyone help me connect to XAMPP localhost from C#?
Install MySQL Connector from Oracle and then use class MySqlConnection.
Here is the download link -
http://www.mysql.com/products/connector/
Download the driver for ADO.Net and the documentation to use it is here -
http://dev.mysql.com/doc/connector-net/en/connector-net-introduction.html
SqlConnection is for MQ SQL server only and thus it is looking for a instance of MS SQL Server to connect to and it fails to connect since none exists. This explains the error message.

LINQ to PostgreSQL errors when using DbLINQ

I'm using LINQ to SQL (PostgreSQL) via DbLINQ.
I have a problem doing LINQ to PostgreSQL. I succesfully generated .dbml and .cs files with dbmetal and I think I got all the references, the code compiles.
Refs:
DbLinq
DbLinq.PostgreSql
DbLinq.SqlServer
Npgsql
using DbLinq.PostgreSql;
I get an exception:
{"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)"}
inner exception:
{"The system cannot find the path specified"}
connection string:
server=127.0.0.1;database=xxxxx;user id=postgres;password=xxxxx;
ili
server=localhost;database=xxxxx;user id=postgres;password=xxxxx;
If I try to use NpgsqlConnectionStringBuilder I get:
HOST=localhost;PORT=5432;PROTOCOL=3;DATABASE=xxxxx;USER
ID=postgres;PASSWORD=xxxxx;SSL=False;SSLMODE=Disable;TIMEOUT=15;SEARCHPATH=;POOLING=True;CONNECTIONLIFETIME=15;MINPOOLSIZE=1;MAXPOOLSIZE=20;SYNCNOTIFICATION=False;COMMANDTIMEOUT=20;ENLIST=False;PRELOADREADER=False;USEEXTENDEDTYPES=False;INTEGRATED SECURITY=False;COMPATIBLE=2.0.12.0;APPLICATIONNAME=
with an exception {"Keyword not supported: 'host'."}
I connect succesfully over SquirrelSQL and jdbc driver on Win7 64-bit, Postgres 9.2 64-bit
Edit:
this is fine
xxxxxDC dc = new xxxxxDC("server=127.0.0.1;database=xxxxx;user id=postgres;password=xxxxx;DbLinqProvider=PostgreSql;");
var q = from r in dc.xxxxx
select r;
but i get an error on
dataGridView1.DataSource = q
Now i know this is because the query is not executed immediately. But the problem remains. "The server was not found or was not accessible"
F1!
lp
You're showing a series of quite different errors.
The first one is because you haven't told LINQ to use nPgSQL, so it's trying to use MS SQL Server and it cannot connect - since there probably isn't any MS SQL Server on the machine. That's why the error says while establishing a connection to SQL Server.
You then show an nPgSQL connection string from NpgsqlConnectionStringBuilder and the resulting error about the host keyword. As best I can guess, that's because the connection string produced by that class is intended for nPgSQL's own connection handling routines, not for LINQ. You need a LINQ connection string that specifies the PostgreSQL provider.
You then show another connection string you say is "fine" that has DbLinqProvider=PostgreSql; appended to it, but say you get "an error" on a statement after that. You do not show the error message, nor do you show the code you used to set the connection up, so we cannot really help you. It's really a different question to what you originally asked anyway; please post a new question for a new problem, rather than rewriting your original question.
You need a LINQ provider for PostgreSQL. See this question and the wikipedia page on LINQ. Look at dbLinq, LINQ to Entities with a PostgreSQL driver for Entity Framework, or dotConnect.
At time of writing, nPgSQL does not include a LINQ provider so you need to add a 3rd party one. AFAIK you cannot simply use LINQ with nPgSQL directly.
I think postgresql uses a different default port than 1433. Try specifying the port 5432
It worked after I used ConnectionStringBuilder class to make the connectionstring.
Thanks for your help!
FYI, DbLinq project is dead ) In my case it didn't work right away. Your schema may work now, but later you'll likely have to invest your time to update the project )

Categories