I have a database created using a code first approach using EF Core v2.2.6. The database is accessed by users via a desktop app and from within a COM registered SolidWorks Add-In(hence the need to use EF Core v2.2.6).
Everything works as expected when reading and writing from the desktop app, but when NavigationEntry::Load is called from within the add-in, I get this exception
This is the configuration class for the Customer class that is added to the ModelBuilderCode:Exception thrown: 'System.InvalidOperationException' in Microsoft.EntityFrameworkCore.dll The type of navigation property 'Projects' on the entity type 'Customer' is 'IReadOnlyCollection<Project>' for which it was not possible to create a concrete instance. Either initialize the property before use, add a public parameterless constructor to the type, or use a type which can be assigned a HashSet<> or List<>.
The Customer classCode:Public Class CustomerConfig Implements IEntityTypeConfiguration(Of Customer) Public Sub Configure(builder As EntityTypeBuilder(Of Customer)) Implements IEntityTypeConfiguration(Of Customer).Configure builder.Metadata.FindNavigation(NameOf(Customer.Projects)) _ .SetPropertyAccessMode(PropertyAccessMode.Field) End Sub End Class
How the NavigationEntry is loadedCode:Public NotInheritable Class Customer Private _Projects As HashSet(Of Project) Public ReadOnly Property Projects As IReadOnlyCollection(Of Project) Get Return _Projects End Get End Property End Class
I am assuming that the issues is because it is being called from within a COM object. I could change how all the classes expose their navigation collections but I would rather not open up those collections even just within the namespace. I do not like to construct the collections in the Db entity class's constructor that is called by the Model Builder because having the navigation collections null is a good way to indicate if the entity has not had it's properties loaded and that it is not just a new entity with an empty collection.Code:Sub LoadProjectCustomer(project as Project, context as DbContext) Dim custNavEnt = context.Entry(project).Navigations _ .First(Function(e) e.Metadata.Name = "Customer") custNavEnt.Load()'<- Exception thrown here when called from COM object End Sub


Reply With Quote
