Control query generate in Entity Framework Core - c#

I have two databases one for testing and one for production for example database and database_test, I set the connection string to connect to database_test, I don't know, why when I try to access to any row of one table the query generated add the database production name for example database.TableName and in the other tables only generate a query with TableName without the databaseName. I try adding a schema in a table name in the model but always is adding the database name that it's the production name, instead of generate the queries without database name, why it's this behavior?
Update: sorry is EF Core 2.2 from mysql with pomelo, the connection string database is testing, the production database name is production. For example the next code
contexto.Table1
is translated in a query select * from production.Table1 instead of only translate in Select * from Table1

Related

SQL MERGE when the two tables are in different Azure databases?

I want to do what SQL MERGE...WHEN MATCHED...WHEN NOT MATCHED does,
but with the source and destination tables on different Azure databases. (Typically on the same server if that helps.)
The source and destination tables are not exactly the same, that's why SQL MERGE would be perfect. (I can decide what column to match on, and which columns to use in the INSERT or UPDATE.)
If this can not be done in SQL, I could load the tables into my C# code and do the merge there (will affect performance though). Does anyone know if .NET has something similar to SQL MERGE in ADO.NET (merging DataTables).
Thanks for any help!
Edit: this image shows the tables before MERGE (all Values are int):
This image shows the MERGE statement and the resulting source and dest:
Azure SQL database doesn't support across database operations directly, even these databases are in the same Azure SQL Server. it will throw below error.
To work around this Azure SQL database only support the across database query with elastic query
Example
CREATE MASTER KEY; -- create master key
GO
-- credential maps to a login or contained user used to connect to remote database
CREATE DATABASE SCOPED CREDENTIAL CrossDbCred1 -- credential name
WITH IDENTITY = 'username', -- login or contained user name
SECRET = '**********'; -- login or contained user password
GO
-- data source to remote Azure SQL Database server and database
CREATE EXTERNAL DATA SOURCE source
WITH
(
TYPE=RDBMS, -- data source type
LOCATION='server.database.windows.net', -- Azure SQL Database server name
DATABASE_NAME='database1', -- database name
CREDENTIAL=CrossDbCred1 -- credential used to connect to server / database
);
GO
-- external table points to table in an external database with the identical structure
CREATE EXTERNAL TABLE [dbo].[source]
(
[Id] [varchar](50),
[value1] [int],
[value2] [int],
[value3] [int]
)
WITH (DATA_SOURCE = [source], -- data source
SCHEMA_NAME = 'dbo', -- external table schema
OBJECT_NAME = 'source' -- name of table in external database
);
GO
Now we can test our Elastic Query to merge tables.
MERGE into destination1 B
USING source E
ON (B.Id = E.Id)
WHEN MATCHED THEN
UPDATE SET value2 = E.value2, value3 = E.value3
WHEN NOT MATCHED THEN
INSERT (Id,value2,value3) VALUES (Id,value2,value3);
Output
Before merging
After merging

how can i create sql database by entity framework code first in specific path

I successfully create my database in entity framework code first using this string :
Data Source=DESKTOP-PL8EDEA;Initial Catalog=MyDB;Integrated Security=True;TimeOut=3600
This top connection string create database in default address of sql server databases
but I want the database to be saved in my desired path
I also tried this field but it was not successful
Data Source=DESKTOP-PL8EDEA;Initial Catalog=|D:\MyFolder|\MyDB;Integrated Security=True;TimeOut=3600
You can specify the path:
Server=.\SQLExpress;AttachDbFilename=C:\MyFolder\MyDataFile.mdf;Database=dbname;Trusted_Connection=Yes;

C# Provider (with EF 6) internal expression mismatch with SQL

Using EF 6 on Net4.5
Posted a Gist showing the internal Expression and the resulting SQL created by the provider. Notice that the joins on Occasion (from BrandVisited) and BrandInfo (also from BrandVisited) are missing. Plus the provider has directly connected up BrandVisited to Respondent via RESP_ID. This is not the intended behavior and yields incorrect SQL.
Any ideas to what is happening behind the scenes?
Running local against SQL Compact but the production target database is SQL Server.
Just to make it clear. BrandVisited has foreign keys (in the EF model) to Occassion which in turn has a foreign key to Respondent. There is NO framework relationship between BrandVisited and Respondent. Despite that the provider (both SQL Compact and SQL Server) associate these table on the Respondent unique key (RESP_ID). How is this even possible?

how to compare two values from different databases in one SQL statements

i have an idea to call two values from two different databases and comapre them in one statement? is it possible?
i am working with c# and MS-SQL
Yes.
For MSSQL you can add the database name in front of your table. You normally have 4 namespaces you can use
[Server name].[database name].[owner].[table_name]
So if you want to compare two values in the one statement you should only need to join across the tables by placing the database name in front of the table name.
If the databases are on different servers then you will need to create a linked server to the side which will run your SQL so that its aware of the other sql server. You can add linked servers in Management studio or via SQL using something like sp_addlinkedserver
You may do cross database joins to compare these values:
SELECT
db1.Value as value1,
db2.Value as value2
FROM
[database1].dbo.MyTable1 as db1
INNER JOIN
[database2].dbo.MyTable as db2
ON
/* insert join clasue */
There are a few possibilities here depending on your setup. If your databases are different SQL Server installations then you will want to look at sp_linkedserver first. Once you have the ability to see both databases using the same login you could just execute the following query where db1 and db2 are the databases, dbo is the owner and tab1 and tab2 are the respective tables.
SELECT a.col1
FROM db1.dbo.tab1 a, db2.dbo.tab2 b
WHERE a.col1 = b.col2
If you should happen to lack the SQL Server permissions to create a linked server, you could create connections to each server and your client could attach to the servers using Microsoft JET library, and then you could perform the heterogeneous join client-side.

How to use different Oracle schemas in Crystal Reports?

Is it possible to use different Oracle schemas in Crystal Reports and change between them at runtime?
I have a report that uses three tables. The report was created using one database user schema. In the other schemas that I have to use, the names of the tables are the same. I need to change between them at runtime. Different users get different data depending on the schema they connect to. I set the servername, the userid, the password and then integratedsecurity to false. If I change/set the DatabaseName it doesn't work.
I use Crystal Reports 2008.
Could you be kind and help me with this?
Thank you very much !
Don't use Crystal's database expert - instead, write SQL queries that do not specify schema. That way, the query will access the tables in the local schema when logged on to it.
eg:
Existing query:
select field1, field2, ...
from user_schema1.datatable
- will select data from user_schema1.datatable, regardless of which schema is logged on.
Amended query:
select field1, field2, ...
from datatable
- will select data from datatable in user_schema1 when logged on to user_schema1, from user_schema2 when logged on to user_schema2, etc.

Categories

Resources