WPF PostgreSQL 11.1
Npgsql.PostgresException: '42P01: relation "testme" does not exist'
When attempting to use a PostgreSQL database with multiple schemas, I have defined the following connection strings in the App.config. Note that the only difference is in the SearchPath:
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.4.0, Culture=neutral" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<clear />
<add name="localconnection" providerName="Npgsql" connectionString="Server=127.0.0.1;Port=5432;Database=chaos;User Id=postgres;Password=****;Searchpath=nova" />
<add name="phoenixconnection" providerName="Npgsql" connectionString="Server=127.0.0.1;Port=5432;Database=chaos;User Id=postgres;Password=****;SearchPath=phoenix;" />
</connectionStrings>
The Npgsql data provider was installed using NuGet: Runtime Version:
v4.0.30319 Version: 4.0.4.0
In PostgreSQL, in the Phoenix schema:
CREATE TABLE phoenix.testme
(
name text COLLATE pg_catalog."default" NOT NULL
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE phoenix.testme
OWNER to postgres;
Using PgAdmin, displaying the testme table works without problem:
select * from phoenix.testme;
I have configured the WCF service using the above connection strings. Using PetaPoco, I write the following script:
public string SayHello()
{
string msg;
using (var db = new chaosDB("phoenixconnection"))
{
var m = db.ExecuteScalar<string>("select version()");
msg = string.Format("Hello from {0}", m);
m = db.ExecuteScalar<string>("select current_schema");
msg = string.Format("{0} Current Schema is {1}", msg, m);
var ss = db.ExecuteScalar<string>("show search_path");
var s = db.Fetch<string>("select * from testme"); <---THIS FAILS!
msg = string.Format("{0} I Am {1}", msg, m);
}
return msg;
}
All works correctly until the "select * from testme" is executed, when I receive the above error. Note: ss from "show search_path" returns correctly with "phoenix"
WHAT AM I DOING WRONG? How do I get this to work??
Any help is most appreciated?
After much head scratching the answer became self-evident. First I had reset the search_path in the database. This did not help. Then I rebuilt the POCO's with PetaPoco and quickly discovered that not only was the new table, "testme", not created, but nor were any POCO's. So, checking, the Database.tt file in PetaPoco showed it to have the wrong ConnectionStringName. Changing the ConnectionStringName to "phoenixconnection" allowed building the POCO's, but again failed to find the "testme" table.
Then the mistake became readily apparent, as stated above, both the "phoenixconnection" and the "localconnection" were pointed to the same port. From previous development, I had PostgreSQL v10.1 running on the same port as the newer PostgreSQL v11.1. Apparently, the first PostgreSQL v10.1 was receiving the connection (and not the newer PostgreSQL v11.1).
Going to services (services.msc) and shutting down v10.1 and running Database.TT now gave the error:
System.InvalidOperationException: Sequence contains more than one matching element
Apparently v10.1 (which I was using for development) only had ONE schema, but v11.1 has multiple schemas. I take the error message to mean that PetaPoco was seeing multiple tables with the same table name--i.e.,it was not distinguishing between schemas.
So, the problem is now solved.
Fix the ports! The older single-schema PostgreSQL v10.1 is kept on port: 5432.
The newer multiple-schema PostgreSQL is kept on port 5433. The v10.1 will be used for the POCO's.
Fix the connection strings in App.config of the WCF so that at run time, the WCF will use the newer v11.1. Once generated, LEAVE THE POCO'S alone and reference them in the WCF file.
Apparently, PetaPoco, can only work with one schema in generating its POCO's, but at runtime will read the connection strings from the App.Config of the WCF to execute its queries, etc. (So in the App.config where Database.TT resides, point PetaPoco to the "development" Database having only a single schema, but in the WCF environment, point the connection string to the new database with multiple schemas. The SearchPath of the connection string IS respected when running through Npgsql).
It would be nice if PetaPoco could generate POCO's specific to a schema in a multi-schema environment, but at the moment, I guess it can't :(
Addendum Note: It turns out that a given instance of PostgreSQL can have multiple DATABASES. So if the connection string for Npgsql is specific to a development database --i.e., a database with only one schema--then during development, PetaPoco works great to create the POCO's. These POCO's can then be directly used in a WCF Service project and uploaded to IIS website. The App.config files of the web site can then be directed to use the run-time database (again in the connection string) to the deployed database. All works well! :)
I'm having a difficult time getting localdb running under IIS in the connection strings. I am running:
Windows 10
SQL Server 2016 Express and LocalDb installed
I added the following to my applicationHost.config, which I would not like to do if possible, but it changed the original error I was getting about not connecting:
<applicationPools>
<add name="myappPool"
autoStart="true" managedRuntimeVersion="v4.0">
// Added this <processModel identityType="ApplicationPoolIdentity" loadUserProfile="true" setProfileEnvironment="true" />**
</add>
<applicationPoolDefaults managedRuntimeVersion="v4.0">
<processModel identityType="ApplicationPoolIdentity"
loadUserProfile="true" setProfileEnvironment="false" />
</applicationPoolDefaults>
</applicationPools>
The myAppPool is running under ApplicationPoolIdentity but changing this to another level of like LocalService doesn't help.
I am now getting this error:
An attempt to attach an auto-named database for file
C:\_sites\mySite\Databases\MyDb.ldf failed. A
database with the same name exists, or specified file cannot be
opened, or it is located on UNC share.
Here is my connection string:
<add name="core"
connectionString="Server=(localdb)\mssqllocaldb;Integrated Security=true; AttachDbFileName=C:\\_sites\mySite\Databases\MyDb.ldf;" />
I can verify that I can connect to (localdb)\mssqllocaldb if Management Studio. I was assuming that localdb meant I did not have to attach the db like I regularly do with sql if using the connection string above. I even added anonymous logon to the file security of the files just to see what would happen.
Any ideas?
I had everything working nicely using EF and code first until I changed the configuration class to add some extra seed data and then tried
Update-Database
Now I get the below error each time.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database. 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.
(provider: SQL Network Interfaces, error: 26 - Error Locating
Server/Instance Specified)
I can connect and view the database in the sql object explorer and even view the tables and data. I'm at a loss and about to give up code first and goto db first or maybe even stored procedures soon.
My connection string looks like this
<add name="MyDbContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDbContext.mdf;Initial Catalog=MyDb;Integrated Security=True" providerName="System.Data.SqlClient" />
Damn, I set the project as the startup project and then ran the command and it looks like its all worked now! Grrrr. I assumed selecting the project from Default project: selection dropdown did this but obviously not.
You can specify connection string via ConnectionString parameter:
Update-Database -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Also you need to use this parameter with the same value for Add-Migration command:
Add-Migration Version_Name -ConnectionString "data source=server_name;initial catalog=db_name;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" -ConnectionProviderName "System.Data.SqlClient" -Verbose
Could someone tell me why my connection string isn't working.. i'm currently using database first, but don't know why it wont connect. Whenever i try i get the error
'The network path was not found'
connection string is:
<add name="FactsAndFiguresEntities1" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string='data source=SERVERNAME User Id=****; Password = **** catalog=FactsAndFigures;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework'" providerName="System.Data.EntityClient" />
The error network path not found, could refer to how you are referencing the metadata files. When you use "res://" EF tries to load the metadata from assemblies in the bin catalogue of the application. Do you have a bin catalogue?
Also I notice that you use both Integrated Security=True and Username/password. The system will use one or the other not both.
For details on EF connection strings see: https://msdn.microsoft.com/en-us/library/vstudio/cc716756(v=vs.100).aspx
I'm trying to track down a SQL Server CE issue that means I cannot use SQL Server CE on a local hard drive of my computer. Clearly the error message is a bogus one as the filename is nowhere near 260 chars
The file resolves to a path that is too long.
The maximum length is 260 characters. [ File name = D:\db.sdf ]
Specifically the D: drive. I have a very simple EF code first app that I want to unit test using SQL Server Compact Edition. When I run the app the first time I try to access the database I get the error above. Originally I tried using a simple filename as the Data Source in the connection string:
<add name="MyContext"
connectionString="Data Source=db.sdf"
providerName="System.Data.SqlServerCe.4.0" />
but got the error.
Then I tried specifying the root drive:
<add name="MyContext"
connectionString="Data Source=D:\db.sdf"
providerName="System.Data.SqlServerCe.4.0" />
and got the same error
If I tried using either a network drive or the c drive:
<add name="MyContext"
connectionString="Data Source=C:\db.sdf"
providerName="System.Data.SqlServerCe.4.0" />
or
<add name="MyContext"
connectionString="Data Source=\\Server\path\db.sdf"
providerName="System.Data.SqlServerCe.4.0" />
The it worked fine and the db was created and the operations performed.
I tried digging around in procmon and all I could find was an Explorer.exe process reporting invalid device for the d: drive.
Any help greatly appreciated
Cheers
Dave
Sounds a bit similar to my problem
I'm using MS Sql Server Management Studio Express R2 SP1...
When I try to create a CE/sdf database on the D:drive it just blinks at me, and asks me for the db path again
I managed to create a CE db on D: via Visual Studio (the inline server manager), but SSMSE still can't access it
If I do it on the C: it works fine.
Bizarre, but I think Sql CE just hates none C drives?