I'm struggling with EF8 and MySQL because EF keeps prepending the default database name specified in connection string to every table name, even if the table is in the different db. Below is the detailed description: I have a MySQL server with multiple databases (db1... db10). The default database is db1 and it is specified in the connection string. The 2 tables participating in the join are in db2 and db3 databases. No matter what I do the sql generated from LINQ ends up as:
SELECT ... FROM db1.db2.Table1 INNER JOIN db1.db3.Table2 or
SELECT ... FROM db1.Table1 INNER JOIN db1.Table2.
I use EF8 and Pomelo EF Core.
I tried setting SchemaBehavior(MySqlSchemaBehavior.Ignore) and MySqlSchemaBehavior.Translate but EF keeps prepending the database name specified in the connection string. If I ommit specifying the default database in the connection string, then an exception will ensue.
Thanks !
Code:
builder.Services.AddDbContext<SubscriberDbContext>(options =>
options.UseMySql(mySQL, new MySqlServerVersion(new Version(8, 0, 0)),
b =>b.SchemaBehavior(MySqlSchemaBehavior.Ignore)
)
.LogTo(Console.WriteLine, LogLevel.Debug)
the default database namethat's not the default database, it's the explicitly specified database that EF connects to. EF isn't a database driver. It's an ORM and deals with entities, not tables. You can't map entities to tables in different databases. After all, some databases don't allow cross-database queries (eg PostgreSQL). You can map to different schemas though.AddDbContextlambda. At this point there may not be enough information to decide what database to use. You'll have to explain what the actual problem is, not how you thought it would be solveddb1that map to the db2 tables, and map your entities to the views. Those views should be updatable as long as they return the primary keys.