When I run the code bellow I get a NullReferenceException at MySql.Data.EntityFramework.SelectStatement.AddColumn.
Edit: I added the stack trace to the bottom of the post and will look into creating a bug report with MySql.Data.EntityFramework. Thank you for the suggestions, but I know what a null reference exception is, and the issue is not that the lists are not initialized. The issue is that the code breaks depending on the number of attributes on my Entity classes.
Breaking Code
using System.Data.Entity;
using System.Linq;
namespace TestProject
{
public class Program
{
static void Main()
{
using (var db = new TestDB())
{
var objects = db.Object1s.Include(obj1 => obj1.Object2s).AsNoTracking().ToList();
}
}
}
}
Class structure
The class structure is simple:
2 base classes, Object1 and Object2.
9 classes that directly inherit from Object2.
There is a many to many relationship between Object1 and Object2
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace TestProject
{
[Table("table1")]
public class Object1
{
public int ID { get; set; }
public string Name { get; set; }
public HashSet<Object2> Object2s { get; set; }
}
[Table("table2")]
public abstract class Object2
{
public int ID { get; set; }
public HashSet<Object1> Object1s { get; set; }
public int? _0 { get; set; }
public int? _1 { get; set; }
public int? _2 { get; set; }
public int? _3 { get; set; }
public int? _4 { get; set; }
public int? _5 { get; set; }
public int? _6 { get; set; }
public int? _7 { get; set; }
public int? _8 { get; set; }
public int? _9 { get; set; }
public int? _10 { get; set; }
public int? _11 { get; set; }
public int? _12 { get; set; }
public int? _13 { get; set; }
public int? _14 { get; set; }
public int? _15 { get; set; }
public int? _16 { get; set; }
public int? _17 { get; set; }
public int? _18 { get; set; }
public int? _19 { get; set; }
}
[Table("table21")]
public class Object21 : Object2
{
public int? _20 { get; set; }
public int? _21 { get; set; }
public int? _22 { get; set; }
public int? _23 { get; set; }
public int? _24 { get; set; }
public int? _25 { get; set; }
public int? _26 { get; set; }
public int? _27 { get; set; }
public int? _28 { get; set; }
public int? _29 { get; set; }
public int? _30 { get; set; }
public int? _31 { get; set; }
public int? _32 { get; set; }
public int? _33 { get; set; }
public int? _34 { get; set; }
public int? _35 { get; set; }
public int? _36 { get; set; }
public int? _37 { get; set; }
public int? _38 { get; set; }
public int? _39 { get; set; }
}
[Table("table22")]
public class Object22 : Object2
{
public int? _40 { get; set; }
public int? _41 { get; set; }
public int? _42 { get; set; }
public int? _43 { get; set; }
public int? _44 { get; set; }
public int? _45 { get; set; }
public int? _46 { get; set; }
public int? _47 { get; set; }
public int? _48 { get; set; }
public int? _49 { get; set; }
public int? _50 { get; set; }
public int? _51 { get; set; }
public int? _52 { get; set; }
public int? _53 { get; set; }
public int? _54 { get; set; }
public int? _55 { get; set; }
public int? _56 { get; set; }
public int? _57 { get; set; }
public int? _58 { get; set; }
public int? _59 { get; set; }
}
[Table("table23")]
public class Object23 : Object2
{
public int? _60 { get; set; }
public int? _61 { get; set; }
public int? _62 { get; set; }
public int? _63 { get; set; }
public int? _64 { get; set; }
public int? _65 { get; set; }
public int? _66 { get; set; }
public int? _67 { get; set; }
public int? _68 { get; set; }
public int? _69 { get; set; }
public int? _70 { get; set; }
public int? _71 { get; set; }
public int? _72 { get; set; }
public int? _73 { get; set; }
public int? _74 { get; set; }
public int? _75 { get; set; }
public int? _76 { get; set; }
public int? _77 { get; set; }
public int? _78 { get; set; }
public int? _79 { get; set; }
}
[Table("table24")]
public class Object24 : Object2
{
public int? _80 { get; set; }
public int? _81 { get; set; }
public int? _82 { get; set; }
public int? _83 { get; set; }
public int? _84 { get; set; }
public int? _85 { get; set; }
public int? _86 { get; set; }
public int? _87 { get; set; }
public int? _88 { get; set; }
public int? _89 { get; set; }
public int? _90 { get; set; }
public int? _91 { get; set; }
public int? _92 { get; set; }
public int? _93 { get; set; }
public int? _94 { get; set; }
public int? _95 { get; set; }
public int? _96 { get; set; }
public int? _97 { get; set; }
public int? _98 { get; set; }
public int? _99 { get; set; }
}
[Table("table25")]
public class Object25 : Object2
{
public int? _100 { get; set; }
public int? _101 { get; set; }
public int? _102 { get; set; }
public int? _103 { get; set; }
public int? _104 { get; set; }
public int? _105 { get; set; }
public int? _106 { get; set; }
public int? _107 { get; set; }
public int? _108 { get; set; }
public int? _109 { get; set; }
public int? _110 { get; set; }
public int? _111 { get; set; }
public int? _112 { get; set; }
public int? _113 { get; set; }
public int? _114 { get; set; }
public int? _115 { get; set; }
public int? _116 { get; set; }
public int? _117 { get; set; }
public int? _118 { get; set; }
public int? _119 { get; set; }
}
[Table("table26")]
public class Object26 : Object2
{
public int? _120 { get; set; }
public int? _121 { get; set; }
public int? _122 { get; set; }
public int? _123 { get; set; }
public int? _124 { get; set; }
public int? _125 { get; set; }
public int? _126 { get; set; }
public int? _127 { get; set; }
public int? _128 { get; set; }
public int? _129 { get; set; }
public int? _130 { get; set; }
public int? _131 { get; set; }
public int? _132 { get; set; }
public int? _133 { get; set; }
public int? _134 { get; set; }
public int? _135 { get; set; }
public int? _136 { get; set; }
public int? _137 { get; set; }
public int? _138 { get; set; }
public int? _139 { get; set; }
}
[Table("table27")]
public class Object27 : Object2
{
public int? _140 { get; set; }
public int? _141 { get; set; }
public int? _142 { get; set; }
public int? _143 { get; set; }
public int? _144 { get; set; }
public int? _145 { get; set; }
public int? _146 { get; set; }
public int? _147 { get; set; }
public int? _148 { get; set; }
public int? _149 { get; set; }
public int? _150 { get; set; }
public int? _151 { get; set; }
public int? _152 { get; set; }
public int? _153 { get; set; }
public int? _154 { get; set; }
public int? _155 { get; set; }
public int? _156 { get; set; }
public int? _157 { get; set; }
public int? _158 { get; set; }
public int? _159 { get; set; }
}
[Table("table28")]
public class Object28 : Object2
{
public int? _160 { get; set; }
public int? _161 { get; set; }
public int? _162 { get; set; }
public int? _163 { get; set; }
public int? _164 { get; set; }
public int? _165 { get; set; }
public int? _166 { get; set; }
public int? _167 { get; set; }
public int? _168 { get; set; }
public int? _169 { get; set; }
public int? _170 { get; set; }
public int? _171 { get; set; }
public int? _172 { get; set; }
public int? _173 { get; set; }
public int? _174 { get; set; }
public int? _175 { get; set; }
public int? _176 { get; set; }
public int? _177 { get; set; }
public int? _178 { get; set; }
public int? _179 { get; set; }
}
[Table("table29")]
public class Object29 : Object2
{
public int? _180 { get; set; }
// This line will cause the program to crash. Remove it and it will work fine.
public int? _181 { get; set; }
}
Database Context
using System.Data.Entity;
namespace TestProject
{
public class TestDB : DbContext
{
public TestDB()
: base("name=TESTDBNAME")
{
}
public virtual DbSet<Object1> Object1s { get; set; }
public virtual DbSet<Object2> Object2s { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Object1>().HasMany<Object2>(obj1 => obj1.Object2s).WithMany(obj2 => obj2.Object1s).Map(m =>
{
m.MapLeftKey("Object1ID");
m.MapRightKey("Object2ID");
m.ToTable("table1_table2");
});
}
}
}
I have tried several things to try to fix the error.
I first tried to remove attributes from the classes to try to pinpoint where the issue is coming from. I expected there to be a mismatch between the C# and the MySQL tables, which I could fix. I found that there was no one type of attribute that was causing the error, rather it is the number of attributes that is making it break. 181 nullable ints seems to be the magic number. This number changes depending on the type of attributes.
I tried moving all of the the attributes onto one class (Object2). I expected this to cause the same error, however it did not. I was able to put more than 181 attributes on the class.
I tried having only two classes in the hierarchy Object2 and Object21 (Object21 inherits from Object2). I put more than 181 attributes on Object2 and ran the code. I was not sure what to expect at this point. The code ran fine.
My take away from all of this is that there is a combination of number of derived classes and member variables on those classes that causes an error in MySql.Data.EntityFramework. I'm at a loss at this point and would appreciate any advice or suggestions. Thanks!
About the system:
OS: macOS Monterey 12.5
.NET version: 4.5.2
Mono Version: 6.12.0.113
Database: MySQL 8.0.31
Entity Framework version: 6.4.4
Database Connector: MySql.Data.EntityFramework 8.0.31
Stack Trace
System.NullReferenceException: Object reference not set to an instance of an object
at MySql.Data.EntityFramework.SelectStatement.AddColumn (MySql.Data.EntityFramework.ColumnFragment column, MySql.Data.EntityFramework.Scope scope) [0x00025] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.Visit (System.Data.Entity.Core.Common.CommandTrees.DbApplyExpression expression) [0x001b5] in <a7df937072044dd3aaaa8bec6fa57762>:0
at System.Data.Entity.Core.Common.CommandTrees.DbApplyExpression.Accept[TResultType] (System.Data.Entity.Core.Common.CommandTrees.DbExpressionVisitor`1[TResultType] visitor) [0x0000c] in <068a3c4080104e51828ab210be168ae8>:0
at MySql.Data.EntityFramework.SqlGenerator.VisitInputExpression (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.VisitInputExpressionEnsureSelect (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.Visit (System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression expression) [0x00022] in <a7df937072044dd3aaaa8bec6fa57762>:0
at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType] (System.Data.Entity.Core.Common.CommandTrees.DbExpressionVisitor`1[TResultType] visitor) [0x0000c] in <068a3c4080104e51828ab210be168ae8>:0
at MySql.Data.EntityFramework.SqlGenerator.VisitInputExpression (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.VisitInputExpressionEnsureSelect (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.Visit (System.Data.Entity.Core.Common.CommandTrees.DbSortExpression expression) [0x00022] in <a7df937072044dd3aaaa8bec6fa57762>:0
at System.Data.Entity.Core.Common.CommandTrees.DbSortExpression.Accept[TResultType] (System.Data.Entity.Core.Common.CommandTrees.DbExpressionVisitor`1[TResultType] visitor) [0x0000c] in <068a3c4080104e51828ab210be168ae8>:0
at MySql.Data.EntityFramework.SqlGenerator.VisitInputExpression (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.VisitInputExpressionEnsureSelect (System.Data.Entity.Core.Common.CommandTrees.DbExpression e, System.String name, System.Data.Entity.Core.Metadata.Edm.TypeUsage type) [0x00000] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.EntityFramework.SelectGenerator.Visit (System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression expression) [0x00022] in <a7df937072044dd3aaaa8bec6fa57762>:0
at System.Data.Entity.Core.Common.CommandTrees.DbProjectExpression.Accept[TResultType] (System.Data.Entity.Core.Common.CommandTrees.DbExpressionVisitor`1[TResultType] visitor) [0x0000c] in <068a3c4080104e51828ab210be168ae8>:0
at MySql.Data.EntityFramework.SelectGenerator.GenerateSQL (System.Data.Entity.Core.Common.CommandTrees.DbCommandTree tree) [0x0001d] in <a7df937072044dd3aaaa8bec6fa57762>:0
at MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition (System.Data.Entity.Core.Common.DbProviderManifest providerManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree) [0x0005e] in <a7df937072044dd3aaaa8bec6fa57762>:0
at System.Data.Entity.Core.Common.DbProviderServices.CreateDbCommandDefinition (System.Data.Entity.Core.Common.DbProviderManifest providerManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext) [0x00000] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition (System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext) [0x00034] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor (System.Data.Common.DbProviderFactory storeProviderFactory, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext, System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver resolver, System.Data.Entity.Core.Query.ResultAssembly.BridgeDataReaderFactory bridgeDataReaderFactory, System.Data.Entity.Core.Query.InternalTrees.ColumnMapFactory columnMapFactory) [0x002a7] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateCommandDefinition (System.Data.Common.DbProviderFactory storeProviderFactory, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext, System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver resolver) [0x00000] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition (System.Data.Entity.Core.Common.DbProviderManifest providerManifest, System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext) [0x00016] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Common.DbProviderServices.CreateCommandDefinition (System.Data.Entity.Core.Common.CommandTrees.DbCommandTree commandTree, System.Data.Entity.Infrastructure.Interception.DbInterceptionContext interceptionContext) [0x00034] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition (System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Common.CommandTrees.DbQueryCommandTree tree) [0x00042] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare (System.Data.Entity.Core.Objects.ObjectContext context, System.Data.Entity.Core.Common.CommandTrees.DbQueryCommandTree tree, System.Type elementType, System.Data.Entity.Core.Objects.MergeOption mergeOption, System.Boolean streaming, System.Data.Entity.Core.Objects.Span span, System.Collections.Generic.IEnumerable`1[T] compiledQueryParameters, System.Data.Entity.Core.Common.Utils.AliasGenerator aliasGenerator) [0x00042] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan (System.Nullable`1[T] forMergeOption) [0x00281] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ObjectQuery`1+<>c__DisplayClass41_0[T].<GetResults>b__1 () [0x0000b] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T] (System.Func`1[TResult] func, System.Data.Entity.Infrastructure.IDbExecutionStrategy executionStrategy, System.Boolean startLocalTransaction, System.Boolean releaseConnectionOnSuccess) [0x00087] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ObjectQuery`1+<>c__DisplayClass41_0[T].<GetResults>b__0 () [0x00046] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult] (System.Func`1[TResult] operation) [0x00000] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ObjectQuery`1[T].GetResults (System.Nullable`1[T] forMergeOption) [0x00093] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Core.Objects.ObjectQuery`1[T].<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__31_0 () [0x00000] in <068a3c4080104e51828ab210be168ae8>:0
at System.Data.Entity.Internal.LazyEnumerator`1[T].MoveNext () [0x00008] in <068a3c4080104e51828ab210be168ae8>:0
at System.Collections.Generic.List`1[T].AddEnumerable (System.Collections.Generic.IEnumerable`1[T] enumerable) [0x00059] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:1108
at System.Collections.Generic.List`1[T]..ctor (System.Collections.Generic.IEnumerable`1[T] collection) [0x00062] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:87
at System.Linq.Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x0000e] in /Users/builder/jenkins/workspace/build-package-osx-mono/2020-02/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/ToCollection.cs:30
at TestProject.Program.Main () [0x00008] in /Users/mikalahmed/Projects/TestProject/TestProject/Program.cs:12
Formatted by https://st.elmah.io