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.

LoadWith used with Nullable<int> foreign key throws exception => "Inheritance mapping is not defined for discriminator value '' "

See original GitHub issue

LoadWith used with an int works fine but the usage of a Nullable<int> foreign key leads to the exception Inheritance mapping is not defined for discriminator value ‘’ in the ‘LinqToDbTest.A’ hierarchy

Exception message:
LinqToDB.Linq.LinqException was unhandled
  HResult=-2146233088
  Message=Inheritance mapping is not defined for discriminator value '' in the 'LinqToDbTest.A' hierarchy.
  Source=linq2db
  StackTrace:
       bei LinqToDB.Linq.Builder.TableBuilder.TableContext.DefaultInheritanceMappingException(Object value, Type type)
       bei lambda_method(Closure , IQueryRunner , IDataReader )
       bei LinqToDB.Linq.QueryRunner.Mapper`1.Map(IQueryRunner queryRunner, IDataReader dataReader)
       bei LinqToDB.Linq.QueryRunner.<ExecuteQuery>d__8`1.MoveNext()
       bei System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       bei System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       bei LinqToDbTest.LinqToDbIssue.LoadTest() in c:\users\documents\visual studio 2015\Projects\LinqToDbTest\LinqToDbTest\TestClass.cs:Zeile 107.
       bei LinqToDbTest.LinqToDbIssue..ctor() in c:\users\documents\visual studio 2015\Projects\LinqToDbTest\LinqToDbTest\TestClass.cs:Zeile 58.
       bei LinqToDbTest.Program.Main(String[] args) in c:\users\documents\visual studio 2015\Projects\LinqToDbTest\LinqToDbTest\Program.cs:Zeile 13.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Steps to reproduce

    public class A
    {
        public A()
        {
            Discriminator = "A";
        }

        public int Id { get; set; }
        public string PropA { get; set; }
        public string Discriminator { get; set; }
    }

    public class B : A
    {
        public B()
        {
            Discriminator = "B";
        }

        public string PropB { get; set; }
    }

    public class C : B
    {
        public C()
        {
            Discriminator = "C";
        }

        public string PropC { get; set; }
    }

    public class Test
    {
        public int Id { get; set; }
        public int? TestAId { get; set; }
        public A TestA { get; set; }
    }

    public class LinqToDbIssue
    {
        public LinqToDbIssue()
        {
            DataConnection.DefaultSettings = new LinqToDbSettings();
            SetMappings();

            InsertData();

            // we can load all rows from A - everything is fine
            var listA = LoadA();

            // exception
            var listTest = LoadTest();
        }

        private void InsertData()
        {
            var b = new B
            {
                Id = 2,
                PropA = "Test B",
                PropB = "Test B"
            };

            var c = new C
            {
                Id = 3,
                PropA = "Test C",
                PropB = "Test C",
                PropC = "Test C"
            };

            var test = new Test
            {
                Id = 1,
                // please note the null here! It's the default I know ;-)
                TestAId = null
            };

            using (var db = new TestDataConnection())
            {
                db.GetTable<A>().Delete();
                db.GetTable<Test>().Delete();

                db.Insert(b);
                db.Insert(c);
                db.Insert(test);
            }
        }

        private List<A> LoadA()
        {
            using (var db = new TestDataConnection())
            {
                return db.GetTable<A>().ToList();
            }
        }

        private List<Test> LoadTest()
        {
            using (var db = new TestDataConnection())
            {
                return db.GetTable<Test>().LoadWith(x => x.TestA).ToList();
            }
        }

        private void SetMappings()
        {
            var mappingBuilder = MappingSchema.Default.GetFluentMappingBuilder();
            mappingBuilder.Entity<A>()
                .HasTableName("A")
                .Inheritance(x => x.Discriminator, "A", typeof(A))
                .Inheritance(x => x.Discriminator, "B", typeof(B))
                .Inheritance(x => x.Discriminator, "C", typeof(C))
                .Property(x => x.PropA).IsColumn().IsNullable().HasColumnName("PropA")
                .Property(x => x.Discriminator).IsDiscriminator().IsColumn().IsNullable(false).HasColumnName("Discriminator")
                .Property(x => x.Id).IsColumn().IsNullable(false).HasColumnName("Id");

            mappingBuilder.Entity<B>()
                .HasTableName("A")
                .Property(x => x.PropB).IsColumn().IsNullable().HasColumnName("PropB");

            mappingBuilder.Entity<C>()
                .HasTableName("A")
                .Property(x => x.PropC).IsColumn().IsNullable().HasColumnName("PropC");

            mappingBuilder.Entity<Test>()
                .HasTableName("Test")
                .Association(x => x.TestA, x => x.TestAId, x => x.Id)
                .Property(x => x.TestAId).IsColumn().IsNullable().HasColumnName("TestAId")
                .Property(x => x.TestA).IsNotColumn();
        }
    }

Environment details

linq2db version: 1.10.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jogibear9988commented, May 6, 2018

in current veta release

0reactions
jogibear9988commented, May 6, 2018

this is fixed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapping Hibernate entity for unknown DiscriminatorValue ...
When Hibernate founds unknown value for the discriminator, then it throws WrongClassException . However, I need to map these unknown ...
Read more >
DLinq Overview
An association relationship is one like a foreign-key to primary-key ... It tells LINQ to SQL which private member is used to hold...
Read more >
Null and not-null @DiscriminatorValue options - In Relation To
Although it can be used for JOINED table inheritance, the @DiscriminatorValue is more common for SINGLE_TABLE inheritance.
Read more >
Entity Framework 6 Recipes
has a discriminator value that does not map the row to exactly one type. This rule can be particularly troubling if you.
Read more >
Zeeshan Hirani - Archives - 2008 - WebLogs @ ASP.NET
When Linq to SQL tries to assign null values to data types that is not defined as null, it throws an exception. Below...
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