Odd Entity Framework Core v2.2.6 NavigationEntry::Load behavior
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
Code:
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<>.
This is the configuration class for the Customer class that is added to the ModelBuilder
Code:
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
The Customer class
Code:
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
How the NavigationEntry is loaded
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
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.
Re: Odd Entity Framework Core v2.2.6 NavigationEntry::Load behavior
Quote:
Originally Posted by
Mad Hat
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
Code:
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<>.
This is the configuration class for the Customer class that is added to the ModelBuilder
Code:
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
The Customer class
Code:
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
How the NavigationEntry is loaded
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
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.
Hello Mat Hat,
I copy-pasted your code and added the Entity Framework Core using this line in the Nuget Package Manager Console "Install-Package Microsoft.EntityFrameworkCore.SqlServer". It seems however there is an object called Project missing. Referred to in the line: "Sub LoadProjectCustomer(project As Project, context As DbContext)" and this code:
Code:
Private _Projects As HashSet(Of Project)
Public ReadOnly Property Projects As IReadOnlyCollection(Of Project)
Get
Return _Projects
End Get
End Property
yours,
Peter Swinkels