Results 1 to 2 of 2

Thread: Odd Entity Framework Core v2.2.6 NavigationEntry::Load behavior

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2018
    Posts
    16

    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.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: Odd Entity Framework Core v2.2.6 NavigationEntry::Load behavior

    Quote Originally Posted by Mad Hat View Post
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width