I am trying to create an entity class which will expose a related collection through a readonly property, like this:
public class MyEntity: Entity
{
public int Id{ get; private set; }
private IList<RelatedEntity> _relatedEntities = new List<RelatedEntity>();
public IReadOnlyList<RelatedEntity> RelatedEntities => _relatedEntities.ToList().AsReadOnly();
}
The builder class looks like this:
public void Configure(EntityTypeBuilder<MyEntity> builder)
{
builder.HasKey(x=>x.Id);
builder.Property<IReadOnlyList<RelatedEntity>>("RelatedEntities")
.HasField("_relatedEntities ")
.UsePropertyAccessMode(PropertyAccessMode.Field);
}
It builds but crashes at runtime with the exception:
InvalidOperationException: The specified field '_relatedEntities' of type 'IList' cannot be used for the property 'MyEntity.RelatedEntities ' of type 'IReadOnlyList'. Only backing fields of types that are assignable from the property type can be used.
Could you provide a working example how to deal with this issue ?
I ckeck this and it worked:
private readonly List<RelatedEntity> _relatedEntitys;
public IReadOnlyCollection<RelatedEntity> RelatedEntitys => _relatedEntitys;
And configuration must be like below:
builder.HasMany(x => x.RelatedEntitys)
.WithOne()
.IsRequired()
.HasForeignKey(x => x.RelatedEntityId)
.OnDelete(DeleteBehavior.Cascade);
builder.Metadata
.FindNavigation("RelatedEntitys")
.SetPropertyAccessMode(PropertyAccessMode.Field);
EF core requires you to use concrete types for backing fields. You need to change your code to:
private readonly List<RelatedEntity> _relatedEntities = new List<RelatedEntity>();
public IReadOnlyList<RelatedEntity> RelatedEntities => _relatedEntities.ToList();
The error message is loud and clear:
IList is not assignable to IReadOnlyList
Changing the property type to the same type as the backing field will do the trick.
Because IEnumerable<T>
is read-only by default, this would be your best bet I believe.
public class MyEntity: Entity
{
public int Id { get; private set; }
private readonly List<RelatedEntity> _relatedEntities = _collection.ToList().AsReadOnly();
public IEnumerable<RelatedEntity> RelatedEntities => _relatedEntities;
}
Update your fluent API as follows:
builder.HasKey(x=>x.Id);
builder.Metadata.FindNavigation("RelatedEntities")
.UsePropertyAccessMode(PropertyAccessMode.Field);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With