Reputation: 733
I've got a scenario where I'm trying to have a growable hierarchy of a same-type nested collection for a specific purpose and I'm using EF Core 2.2.
public class Group:Entity
{
public Group(Guid id):base(id)
{
}
...
public List<Group> SubGroups { get; set; }
}
public abstract class Entity
{
protected Entity(Guid id)
{
Id = id;
}
public Guid Id { get; private set; }
}
The goal is to save data like:
|-GrandParent Group
-Parent Group
|--Child1 Group
---GrandChild1 Group
|--Child2 Group
---GrandChild2 Group
ERROR
{System.InvalidOperationException: No suitable constructor found for entity type 'Group'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'guid' in 'Group(Guid guid)'.
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConstructorBindingConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuilder)
Could you please let me know how I could achieve this?
Upvotes: 1
Views: 107
Reputation: 205539
The issue has nothing to do with nested collection, but the entity constructor, and is not reproduced with the sample from the question.
But the exception message
No suitable constructor found for entity type 'Group'. The following constructors had parameters that could not be bound to properties of the entity type: cannot bind 'guid' in 'Group(Guid guid)'.
indicates that in your real code you have used
public Group(Guid guid):base(guid)
The problem is the name of the parameter guid
(instead of id
). As explained in the Entity types with constructors (inside Some things to note):
The parameter types and names must match property types and names, except that properties can be Pascal-cased while the parameters are camel-cased.
In this case, the property is called Id
, hence the parameter must be called id
as in the post.
Upvotes: 2