Null reference exception on insert of derived class - what am I doing wrong?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
dgilmour77
Posts: 4
Joined: Thu 28 Feb 2019 23:31

Null reference exception on insert of derived class - what am I doing wrong?

Post by dgilmour77 » Thu 05 Mar 2020 05:35

The code below attempts to add an object to a MySQL database using EntityDeveloper 6.6.857 in Visual Studio 2019. The code is very simple and shown below. A null reference exception is thrown upon calling InsertOnSubmit. The stack trace doesn't say much. Casting to the base class doesn't fix the problem. Could this be a bug in EntityDeveloper? Thanks -

Code: Select all

   at Devart.Data.Linq.Table.f(Object A_0)
   at Devart.Data.Linq.Table`1.InsertOnSubmit(TEntity entity)
   at DevartEntityFrameworkBugDemo.Demo.Run()
 
Here is the code:

Code: Select all

     static class Demo
    {
        public static void Run()
        {
            Context.DataContext dataContext = new Context.DataContext();

            // [1] This works fine
            Context.Item item = new Context.Item();
            item.ID = Guid.NewGuid();                   // This is the primary key, and cannot be null
            item.ItemName = "Table";                    // Just a nullable field
            item.Description = "Flat and level";        // Another nullable field

            dataContext.Items.InsertOnSubmit(item);     // No problem here
            dataContext.SubmitChanges();

            // [2] But this doesn't

            EnhancedItem enhancedItem = new EnhancedItem(Guid.NewGuid(), "Bookcase", "For books");

            // This throws a null reference exception:
            dataContext.Items.InsertOnSubmit(enhancedItem);

            // This doesn't fix ths problem
            //Context.Item castVersion = (Context.Item)enhancedItem;
            //dataContext.Items.InsertOnSubmit(castVersion);

            // This doesn't fix the problem
            //dataContext.Items.InsertOnSubmit((Context.Item)enhancedItem);

            // (We never get here)
            dataContext.SubmitChanges();
        }

        /// <summary>
        /// The EnhancedItem class derives from the class created by EntityDeveloper
        /// </summary>
        public class EnhancedItem : Context.Item
        {
            public EnhancedItem(Guid id, string itemName, string description)
            {
                ID = id;
                ItemName = itemName;
                Description = description;
            }
        }
    }

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Null reference exception on insert of derived class - what am I doing wrong?

Post by Shalex » Sat 07 Mar 2020 16:33

Thank you for your report. We have reproduced the issue and are investigating it. We will notify you about the result.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Null reference exception on insert of derived class - what am I doing wrong?

Post by Shalex » Mon 30 Mar 2020 22:31

* The bug with throwing System.NullReferenceException, when constructing the error saying that unmapped class is not supported, is fixed
* The insert of the object, the type of which is an unmapped class inherited from a mapped class, is supported

To use the feature, you should initialize the properties of a base class in a derived class. We will notify you when the new build is available for download.

As a workaround with a current build, you can employ AutoMapper:

Code: Select all

    // dataContext.Items.InsertOnSubmit(enhancedItem);

    var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<enhancedItem, Item>());
    var mapper = config.CreateMapper();
    dataContext.Items.InsertOnSubmit(mapper.Map<Item>(enhancedItem));

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Null reference exception on insert of derived class - what am I doing wrong?

Post by Shalex » Sat 04 Apr 2020 11:36

New build of dotConnect for MySQL 8.17.1612 is available for download now: viewtopic.php?f=2&t=40465.

Post Reply