question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MySql: SqlNullValueException Generating Models

See original GitHub issue

Generating my datamodels is firing an SqlNullValueException when getting my schema (magento). Does anyone know how to resolve this?

CODE:

    var connectionString = "Server=localhost;Port=3306;Database=magento;Uid=root;Pwd=1234;charset=utf8;ConvertZeroDatetime=True;AllowZeroDatetime=True;";

     // not working
    var sp = dataConnection.DataProvider.GetSchemaProvider();
    var db = sp.GetSchema(dataConnection);

    // working 
    var connection = new MySqlConnection(connectionString);
    var db2 = connection.GetSchema();

    // working - extra
    var command = connection.CreateCommand();
    command.CommandText = "SHOW TABLES;";

    var reader = command.ExecuteReader();

    while (reader.Read())
    {
        var row = "";
        for (var i = 0; i < reader.FieldCount; i++)
            row += reader.GetValue(i).ToString() + ", ";
        Console.WriteLine(row);
    }
    connection.Close();

ERROR: Running transformation: System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values. at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index, Boolean checkNull) at MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32 i) at MySql.Data.MySqlClient.MySqlDataReader.GetString(String column) at MySql.Data.MySqlClient.SchemaProvider.GetIndexColumns(String[] restrictions) at MySql.Data.MySqlClient.SchemaProvider.GetSchemaInternal(String collection, String[] restrictions) at MySql.Data.MySqlClient.ISSchemaProvider.GetSchemaInternal(String collection, String[] restrictions) at MySql.Data.MySqlClient.SchemaProvider.GetSchema(String collection, String[] restrictions) at MySql.Data.MySqlClient.MySqlConnection.GetSchema(String collectionName, String[] restrictionValues) at MySql.Data.MySqlClient.MySqlConnection.GetSchema(String collectionName) at LinqToDB.DataProvider.MySql.MySqlSchemaProvider.GetPrimaryKeys(DataConnection dataConnection) at LinqToDB.SchemaProvider.SchemaProviderBase.GetSchema(DataConnection dataConnection, GetSchemaOptions options) at Microsoft.VisualStudio.TextTemplatingB41FF171F1D676BBB07B6DF817D0F73ED0CBC95FB4CC7ECDC5E60FA2EE25F117EC5BF9EE7E5389A250AC2F106B5484E253B1CA37CEE331162BC4836EB5843F12.GeneratedTextTransformation.LoadServerMetadata(DataConnection dataConnection) in C:\Users\Wesley.PACOLIBA\Documents\## Code\ERP\intershoe\TestMagento - LinqToDb\LinqToDB.Templates\DataModel.ttinclude:line 56 at Microsoft.VisualStudio.TextTemplatingB41FF171F1D676BBB07B6DF817D0F73ED0CBC95FB4CC7ECDC5E60FA2EE25F117EC5BF9EE7E5389A250AC2F106B5484E253B1CA37CEE331162BC4836EB5843F12.GeneratedTextTransformation.LoadMetadata(DataConnection dataConnection) in C:\Users\Wesley.PACOLIBA\Documents\## Code\ERP\intershoe\TestMagento - LinqToDb\LinqToDB.Templates\DataModel.ttinclude:line 310 at Microsoft.VisualStudio.TextTemplatingB41FF171F1D676BBB07B6DF817D0F73ED0CBC95FB4CC7ECDC5E60FA2EE25F117EC5BF9EE7E5389A250AC2F106B5484E253B1CA37CEE331162BC4836EB5843F12.GeneratedTextTransformation.LoadMySqlMetadata(String server, String database, String uid, String password, Int32 port) in C:\Users\Wesley.PACOLIBA\Documents\## Code\ERP\intershoe\TestMagento - LinqToDb\LinqToDB.Templates\LinqToDB.MySql.ttinclude:line 28 at Microsoft.VisualStudio.TextTemplatingB41FF171F1D676BBB07B6DF817D0F73ED0CBC95FB4CC7ECDC5E60FA2EE25F117EC5BF9EE7E5389A250AC2F106B5484E253B1CA37CEE331162BC4836EB5843F12.GeneratedTextTransformation.TransformText() in C:\Users\Wesley.PACOLIBA\Documents\## Code\ERP\intershoe\TestMagento - LinqToDb\DataModels\Magento.tt:line 38 TestMagento - LinqToDb C:\Users\Wesley.PACOLIBA\Documents\## Code\ERP\intershoe\TestMagento - LinqToDb\LinqToDB.Templates\DataModel.ttinclude 56

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Feb 16, 2017

Try to ensure that your indexes have all the following fields NOT NULL. Here is decompiled source of problematic method.

    public virtual MySqlSchemaCollection GetIndexColumns(string[] restrictions)
    {
      MySqlSchemaCollection schemaCollection = new MySqlSchemaCollection("IndexColumns");
      schemaCollection.AddColumn("INDEX_CATALOG", typeof (string));
      schemaCollection.AddColumn("INDEX_SCHEMA", typeof (string));
      schemaCollection.AddColumn("INDEX_NAME", typeof (string));
      schemaCollection.AddColumn("TABLE_NAME", typeof (string));
      schemaCollection.AddColumn("COLUMN_NAME", typeof (string));
      schemaCollection.AddColumn("ORDINAL_POSITION", typeof (int));
      schemaCollection.AddColumn("SORT_ORDER", typeof (string));
      string[] restrictions1 = new string[Math.Max(restrictions == null ? 4 : restrictions.Length, 4)];
      if (restrictions != null)
        restrictions.CopyTo((Array) restrictions1, 0);
      restrictions1[3] = "BASE TABLE";
      foreach (MySqlSchemaRow row in (IEnumerable<MySqlSchemaRow>) this.GetTables(restrictions1).Rows)
      {
        using (MySqlDataReader reader = new MySqlCommand(string.Format("SHOW INDEX FROM `{0}`.`{1}`", row["TABLE_SCHEMA"], row["TABLE_NAME"]), this.connection).ExecuteReader())
        {
          while (reader.Read())
          {
            string str1 = SchemaProvider.GetString(reader, reader.GetOrdinal("KEY_NAME"));
            string str2 = SchemaProvider.GetString(reader, reader.GetOrdinal("COLUMN_NAME"));
            if (restrictions == null || (restrictions.Length < 4 || restrictions[3] == null || !(str1 != restrictions[3])) && (restrictions.Length < 5 || restrictions[4] == null || !(str2 != restrictions[4])))
            {
              MySqlSchemaRow mySqlSchemaRow = schemaCollection.AddRow();
              mySqlSchemaRow["INDEX_CATALOG"] = (object) null;
              mySqlSchemaRow["INDEX_SCHEMA"] = row["TABLE_SCHEMA"];
              mySqlSchemaRow["INDEX_NAME"] = (object) str1;
              mySqlSchemaRow["TABLE_NAME"] = (object) SchemaProvider.GetString(reader, reader.GetOrdinal("TABLE"));
              mySqlSchemaRow["COLUMN_NAME"] = (object) str2;
              mySqlSchemaRow["ORDINAL_POSITION"] = reader.GetValue(reader.GetOrdinal("SEQ_IN_INDEX"));
              mySqlSchemaRow["SORT_ORDER"] = (object) reader.GetString("COLLATION");
            }
          }
        }
      }
      return schemaCollection;
    }

For example and probably it’s a problem:

reader.GetString("COLLATION")
1reaction
sdanylivcommented, Feb 16, 2017

It’s a known bug https://bugs.mysql.com/bug.php?id=75301

How to repeat: Define a full text search index on any table and then run the following code

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Core: `SqlNullValueException: Data is ...
I was using a SQL view instead of a table and non of my fields had the [Required] tagged on them in the...
Read more >
Data is Null. This method or property cannot be called
SqlNullValueException : Data is Null. This method or property ... Create ASP.NET Core Web Application ... SQL vs NoSQL or MySQL vs MongoDB....
Read more >
Data is Null. This method or property cannot be called on ...
I'm trying to run an API project and it gives me an error like this: Here is my code inside the API Models:...
Read more >
7.2.1 Creating a Database with Code First in EF Core
The Code First approach enables you to define an entity model in code, create a database from the model, and then add data...
Read more >
7.1 Entity Framework 6 Support
Idempotent migrations scripts allow you to generate an SQL script that can upgrade a database at any version up to the latest version....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found