Bug #94359 Table schema ignored if EF6, but code first migrations uses it as it should
Submitted: 16 Feb 2019 0:12 Modified: 8 Apr 2019 19:38
Reporter: Spartak Timchev Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:8.0.15.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: entityframework

[16 Feb 2019 0:12] Spartak Timchev
Description:
When table and schema names are both provided for entyty, either by code or by entity attribute, migrations are using the schema name as it shoud, creating for example "myschema.mytable" table. When I try to insert/update/delete from table using EF, the schema name is ignored and there is only "mytable" in sql query, resulting in exception.

It seems very old bug present form version 6.9.9, but despite the very resolution, it is still present in version 8.0.15.0.

How to repeat:
1. In code first migrations project, set table name and schema name explicitly for entity either by:
[Table("myTable", Schema="MySchema")]
public class MyEntity
{
...
}

or in "OnModelCreating" method of DBContext:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
...
    modelBuilder.Entity<MyEntity>().ToTable("MyTable", "MySchema");
...
}

2. Start project with missing database and try to use database in order for it to be created and migration to start.

The migration will create "myschema.mytable" table in database, as it shoud, but then when trying to access this table with EF, the exception will be thrown.

Suggested fix:
In "TableFragment.cs", "WriteSql" method on line 49 is:

        public override void WriteSql(StringBuilder sql)
        {
            if (DefiningQuery != null)
                sql.AppendFormat("({0})", DefiningQuery);
            else
                sql.AppendFormat("{0}", QuoteIdentifier(Table));
            base.WriteSql(sql);
        }

It should be replaced with:
        public override void WriteSql(StringBuilder sql)
        {
            if (DefiningQuery != null)
                sql.AppendFormat("({0})", DefiningQuery);
            else
            {
                if (string.IsNullOrWhiteSpace(Schema) || Schema.Equals("dbo", System.StringComparison.InvariantCultureIgnoreCase))
                {
                    sql.AppendFormat("{0}", QuoteIdentifier(Table));
                }
                else
                {
                    sql.AppendFormat("{0}", QuoteIdentifier($"{Schema}.{Table}"));
                }
            }
            base.WriteSql(sql);
        }
[16 Feb 2019 0:14] Spartak Timchev
"TableFragment.cs" file with repaired "WriteSql" method. Nothing else is changed.

Attachment: TableFragment.cs (text/plain), 2.86 KiB.

[8 Mar 2019 19:38] MySQL Verification Team
Please provide a complete repeatable test case (example) client file.
[9 Apr 2019 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".