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

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?

Related

EF Core talking to postgres with citext datatype

I need some assistance.
I'm running
.NET Core 3.1
Npgsql.EntityFrameworkCore.PostgreSQL 3.1.2
Microsoft.EntityFrameworkCore.Tools 3.1.2
against a postgres db
PostgreSQL 11.5
We moved from MSSQL to postgres, and previously this code was working:
_context.Users.SingleOrDefaultAsync(x => x.EmailAddress == emailadress);
With that said, now the data is case sensitive and e.g. comparing "john.doe#mail.com" with "John.Doe#mail.com" stopped working.
One could change all to lower -strings, but as there is a lot of these kinds of comparisons, it would take some time to change this.
Then I found citext to the rescue, I thought.
I have altered the table/column and then there is the .net entity I'm a bit insecure of how fix.
I tried to add to the entity, based on this link:
https://www.npgsql.org/efcore/mapping/general.html?tabs=data-annotations
[Column(TypeName = "citext")]
public string EmailAddress { get; set; }
but it didn't help.
I find this in the logs:
Executing DbCommand [Parameters=[#__emailadress_0='?'], CommandType='Text', CommandTimeout='30']
And I don't know if I should expect the CommandType='citext' here or not.
Do anyone have any input regarding this?
Thanks alot!
Ensure that a new migration with that type change is applied to your database.
Another thing to consider is the collation used by your database. What I think is that just after you have migrated from MSSQL to Postgre the collation has not been preserved.
The user used to connect to the Postgres database didn't have default schema set. The data/tables used, didn't exist in the default schema set for the database. After the preferred/default schema was set on the user used, to connect to the database, the citext comparison started to work.
ALTER ROLE myUser IN DATABASE myDatabase SET search_path TO mySchema;

Control query generate in Entity Framework Core

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

C# switch from local db to external (Azure) db causes errors

I have used the built in Database that comes with Visual studio by using Code First with Entity Framework. Now I wanted to move to an external database so I created one and saved the connections string. So I connected to my azure database by supplying the connection string in the db context constructor. Now though, the problem is that Entity Framework isn't able to create the necessary tables. When I run my application and try to access something, I get System.Data.SqlClient.SqlException: 'Either the parameter #objname is ambiguous or the claimed #objtype (COLUMN) is wrong.'
And I assume this is beacuse my azure db is empty. Why doesn't Entity Framework create the tables?
The error was that I had forgotten to run migration on the new database:
*Add-Migration newDb
*Update-Database

Entity Framework with IBM DB2

We have a DB2 database which we are accessing via EF. We are able to connect to the database and do read & write operations as part of this.
Now the plan is to initialize the DB using
Database.SetInitializer(new CreateDatabaseIfNotExists<CustomContext>())
This throws out an error saying
HResult=-2146232032
Message=CreateDatabase is not supported by the provider.
Source=EntityFramework InnerException:
System.Data.Entity.Core.ProviderIncompatibleException
Previously we were connecting with
Database.SetInitializer(new NullDatabaseInitializer<CustomContext>());
and this was working fine.
The question is has any one tried creating a new DB2 database from within EF?
You cannot do that.That is Known limitation of the provider.
General limitations:
Only database-first scenarios are supported: any database object that
you reference in Entity Framework must first exist in the database.
Invocation of store-specific functions is not supported.
Trusted context connection properties that you set in the Server Explorer Add
Connection dialog are not passed to Entity Framework connections.
You can read it here : Limitations to Microsoft Entity Framework support
Migration is not supported by IBM EF provider implementation.
If you need DB2 migration support you can use this package that implements only migration (so you can use it in addition to IBM DB2 EF Provider)
https://www.nuget.org/packages/System.Data.DB2.EntityFramework.Migrations/
You can find more info here
https://db2ef6migrations.codeplex.com/

How to get PK info from SQL Server Compact

My company is using SQL Server Compact to store and manage a local database and I need to get the primary key information from the database tables.
I have used OleDbConnection.GetSchema() in the past to get this information from Access databases using Jet and OLE drivers. But it appears that SqlCeConnection.GetSchema() throws a NotImplementedException (or rather that it returns DBConnection.GetSchema() which throws the same exception).
I don't need all of the information normally returned by GetSchema(), just the names of the PK's. Is there a good way to find out the Primary Key for a table in a SQL Server Compact database?
NOTE: The above method references are not static function calls. Consider the class name to be an object reference of that type.
SELECT INDEX_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.INDEXES
WHERE PRIMARY_KEY = 1
AND TABLE_NAME = 'MyTable';

Categories

Resources