Bug #73205 Schema name not used by EF
Submitted: 5 Jul 2014 11:55 Modified: 5 Feb 2016 13:18
Reporter: Laredo Tirnanic Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.8.3.0, 6.9.8 OS:Microsoft Windows
Assigned to: CPU Architecture:Any

[5 Jul 2014 11:55] Laredo Tirnanic
Description:
When using EF (code first) I mapped a table(test) to a schema called core.

modelBuilder.Entity<Test>().Map(x => x.ToTable("test", "core"));

When I run update-database it creates a table called core.test as expected. 

However when I try and access context.Tests it still seems to be mapped to 'test1.test'. So it seems to be ignoring the schema.

I have tested this with Mssql and it works as expected there.      

How to repeat:
public class Test
    {
        public int Id { get; set; }
        public string Description { get; set; }
    }

[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class MySqlDbContext : DbContext
    {
        public DbSet<Test> Tests { get; set; }

        
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            //Todo: Add index information, more relationships, etc
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        
            modelBuilder.Entity<Test>().Map(x => x.ToTable("test", "core"));
        }

        public MySqlDbContext()
            : base("name=CodeFirst")
        {

        }
}

    internal sealed class Configuration : DbMigrationsConfiguration<MySqlDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "DataServices.Server.Domain.Repositories.nFrallAppMSSQLDbContext1";
            AutomaticMigrationDataLossAllowed = true;

            SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
            CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
        }
}

In package manager run: 

update-database -verbose

This will create the table as expected.

In global.asax.cs add the following:

MySqlDbContext context = new MySqlDbContext();

            context.Database.Initialize(false);
            var b = context.Tests.Any(a => a.Description == "Hello");

When querying Tests it will give an error that it can't find the table 'test1.test'. It should be searching for 'test1.core.test'
[13 Nov 2014 11:30] Chiranjeevi Battula
Hello Laredo Tirnanic,

Thank you for the bug report.
I could not repeat the issue on Visual Studio 2013 (C#.Net) with  MySQL Connector/Net 6.9.3. Could you please provide complete repeatable test case to confirm this issue at our end?

Thanks,
Chiranjeevi.
[14 Dec 2014 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".
[26 Jan 2016 22:07] Kai Scheddin
May it could be an equal fix to the bug #80156 
(http://bugs.mysql.com/bug.php?id=80156)
at TableFragment.cs.
[5 Feb 2016 13:18] Chiranjeevi Battula
Hello  Laredo Tirnanic,

Thank you for your feedback.
Verified this behavior on Visual Studio 2013 (C#.Net) with  MySQL Connector/Net 6.9.8.

Thanks,
Chiranjeevi.
[5 Feb 2016 13:21] Chiranjeevi Battula
http://bugs.mysql.com/bug.php?id=80156 marked as duplicate of this one.
[5 Aug 2016 21:05] Spartak Timchev
Same behavior confirmed on version 6.9.9
[4 Jul 2017 10:29] Piotr Qx
I appears this issue still exists in the code and it's an easy fix. I fixed it locally with the following code which I will submit but need to know when will the fix be released.

I changed the method as follows. I'm not sure why it hasn't been fixed in over a year and new branches have the issue too.

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

            if (schemaName.ToLowerInvariant().Equals("dbo"))
            {
                schemaName = "";
            }
                
            if (!string.IsNullOrEmpty(schemaName) && !string.IsNullOrWhiteSpace(schemaName))
            {
                tableName = string.Format("{0}.{1}",QuoteIdentifier(Schema), tableName);
            }
            sql.AppendFormat("{0}", QuoteIdentifier(tableName));
        }
        
      base.WriteSql(sql);
    }