Schema error when running update-database command to create identity tables for npgsql data provider - npgsql

I am manually creating the identity tables for a new ASP.NET 6 project following this tutorial. Such tutorial is for MS SQL Server and I am using PostgreSQL so I made the appropriate modifications. Although the process is straightforward, I have a problem when reaching the update-database step where I get the following error: "Couldn´t set schema (Parameter ´schema')". My connection string is as follows: "host=localhost; database=testdb001; schema=testdb001; port=5433; user id=some-user; password=some-password;". I found that such error disappears and the identity tables are successfully created if I remove the schema parameter in the connection string but tables are created in the public schema that PostgreSQL automatically includes when a new DB is created. However, I do not want this to happen because I want to use another schema name. I visited connection strings website for PostgreSQL (https://www.connectionstrings.com/postgresql/) and I see a link for Npgsql, and all the examples here do not contain the schema parameter. This is the first time that I use npgsql. Is there a way to create the identity tables in a specific schema name?
Maybe a workaround is to specify a schema name in the search_path parameter in postgresql.conf file but this would lead to add a name every time I define a new schema. I think that the schema name in the connection string is a great choice but I wonder why this is not accepted for npgsql.
Respectfully,
Jorge Maldonado

I found the solution.
For npgsql you must use search path as a connection string parameter instead of schema. So a connection string is as follows
"host=localhost; database=testdb001; search path=testdb001; port=5433; user id=some-user; password=some-password;"
and not
"host=localhost; database=testdb001; schema=testdb001; port=5433; user id=some-user; password=some-password;"
Regards,
Jorge Maldonado

Related

Error when copying CSV files from Windows directory into SQL Server DB by using Apache NiFi

I am trying to copy CSV files from my local directory into a SQL Server database running in my local machine by using Apache NiFi.
I am new to the tool and I have been spending few days googling and building my flow. I managed to connect to source and destination but still I am not able to populate the database since I get the following error: "None of the fields in the record map to the columns defined by the tablename table."
I have been struggling with this for a while and I have not been able to find a solution in the Web. Any hint would be highly appreciated.
Here are further details.
I have built a simple flow using GetFile and PutDatabaseRecord processors 1.
My input is a simple table with 8 columns 2.
My configurations for GetCSV process are here (I have added the input directory and left the rest as default): 3
The configuration for PutDatabaseRecord process is here (I have referred to the CSVReader and DBCPConnectionPool controller services, used the MS SQL 2012+ database type (I have 2019 version), configured INSERT statement type, inserted the schema and correct table name and left everything else as default): 4
The CSVReader configuration looks as shown here (Schema Access Strategy = Use String Fields From Header; CSV Format = Microsoft Excel): 5
And this is the configuration of the DBCPConnectionPool (I have added the correct URL, DB driver class name, driver location, DB user and password): 6
Finally, this is a snapshot of the description of the table I have created in the database to host the content: 7
Many thanks in advance!
The warning "None of the fields in the record map to the columns defined by the tablename table." is also obtained when the processor is not able to find the table and this can happen also when the table name is correctly configured in PutDatabaseRecord but there is some issue with user access rights (which ended up to be the actual cause of my error ...).

Entity Framework Data First - Invalid Object Name dbo.TableName

sup?
I got this problem wich has been slowing down my production a lot, hope u guys can give me any tip on how to solve it....
I'm currently using EF6 and a custom ConnectionString with a connectionString builder class that reads and external xml file.
My database is all set up and running in SQL 2010 and Management Studio ok, but whenever I generate the edmx file, it generates the class mappings okay, I've compared the original ConnectionString to my ConnectionString Builder they're the same, changed the :base to receive a custom ConnectionString, all set.
But when I try to save changes it doesn't find the table object, I have deleted it, created again from the start, still same "Invalid database object Dbo.TableName" error.
Does any one have a clue on this?
Thanks in Advance!
This error is not coming from Entity Framework but directly from the database.
Use SQL Profiler and include in the trace the "DatabaseName" and "ServerName" and you will find out it's not the same as your expected.
The only other options which this issue happen is if your "tableName" is not the same as the table name in SQL, so run the SQL from the SQL Profiler directly into SSMS.

How to change application name with Entity Framework

In the SQL Management Studio, ever connection has an Application listed along with it... for every EF connection I have it just says "EntityFramework" as the application name. How do I alter this so I can actually put the name of the app in this field?
http://blogs.msdn.com/b/dditweb/archive/2006/03/02/542151.aspx
From the article:
Here's a great trick that can be really handy if you're a fan of the
SQL Profiler. In your application connection strings add the
"Application Name" keyword/value. Example.
server=MyServer;database=MyDatabase;Integrated Security=SSPI;Application Name=My Application;
After this is done,
fire up SQL Profiler and you'll notice that the profiled events are
categorized under the name you provide rather than the default Data
Provider name.
Edit:
If you look at your current entity framework connection string near the end of the string (assuming you're using an autogenerated string) you should already see an "Application name=EntityFramework. You can just change that to your desired application name and boom!

Changing database name for existing Asp.net MVC3 project

I am working on Asp.net MVC 3 project, I need change the database name for the project I tried changing the database name in connection string & What happens is that when I pull the data it pulls from new DB where as when I try to insert data in some table it tries to insert in the old db. I am sure from where it is getting reference to the old DB name. Please HELP.
There must be a different connection string for the inserts. You should make a search in the entier solution after the database name or parts of the connection string to find that connection string and modify it too.

Setting up a Entity Framework Code First Database on SQL Server 2008

I successfully created my first Entity Framework Code first project using SQL Server CE. The database tables were created automatically and all was well.
I then changed my connection string to point to a SQL Server database I'd created and expected it to autogenerate the tables. It didn't and instead complained about the tables not existing.
After some reading I found that I needed to add an initializer to my App_Start method.
I tried:
// Method 1
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EFContext>());
But this didn't work at first because it couldn't find an EdmMetaData table to discover if the model had changed.
So I tried adding:
// Method 2
Database.SetInitializer(new CreateDatabaseIfNotExists<EFContext>());
But this didn't work because I'd already created the database, and when I removed the database it still didn't work because the user I was logging in as didn't have create database privileges.
I finally tried:
// Method 3
Database.SetInitializer(new DropCreateDatabaseAlways<EFContext>());
Which worked, but I don't really want to drop the db every time. So once it ran and added the EdmMetaData table I replaced it with method 1 and that now works fine.
Although this method works, it doesn't feel as elegant as when I was using SQL Server CE and I've not found someone suggesting that you use this method.
So, am I missing something and is there an easier, more elegant way to do this?
Method 1 and 2 expects that database doesn't exist yet. They don't work if you create database manually. If you need EF to create tables in existing database you must create custom database initializer.

Resources