'Collection is read-only.' 尝试初始化实体时

'Collection is read-only.' when trying to initialise entities

我只想从数据库中获取我的实体:

var sendingRules = context.Set<SendingRules>().ToList();

但是自从我向 SendingRules 添加了一个自有类型后,我收到了这条恼人的消息:

System.NotSupportedException: 'Collection is read-only.'

有问题的实体看起来像这样

public sealed class SendingRules : Entity<Guid>
{
    private readonly List<SendingRuleLBACFilter> _lbacFilters = new();

    private SendingRules() { }

    public SendingRules(LBACLevels? lbacLevel = null) : base(Guid.NewGuid())
    {
        LBACLevel = lbacLevel;
    }

    public LBACLevels? LBACLevel { get; private set; }

    public IReadOnlyCollection<SendingRuleLBACFilter> LBACFilters => _lbacFilters.AsReadOnly();

    public void AddLBACFilter(SendingRuleLBACFilter lbacFilter)
    {
        EnsureArg.IsNotNull(lbacFilter, nameof(lbacFilter));

        _lbacFilters.Add(lbacFilter);
    }
}

在哪里

public sealed class SendingRuleLBACFilter : SendingRuleFilter
{
    public SendingRuleLBACFilter(string template) : base(template)
    {
    }
}

这是我的流畅配置:

builder
    .OwnsMany(
        (sendingRule) => sendingRule.LBACFilters,
        (builder) =>
        {
            builder.ToTable("SendingRuleLBACFilters");
            builder.WithOwner()
                .HasForeignKey("SendingRulesId");
        });
        

我尝试将 .Navigation("_lbacFilters") 添加到该配置调用的末尾,但随后它抱怨找不到 属性(即使设置为 public.. .)

System.InvalidOperationException: Navigation 'SendingRules._lbacFilters' was not found. Please add the navigation to the entity type before configuring it

有人知道问题出在哪里吗?

at System.ThrowHelper.ThrowNotSupportedException(ExceptionResource resource)
at System.Collections.ObjectModel.ReadOnlyCollection`1.System.Collections.Generic.ICollection<T>.Add(T value)
at Microsoft.EntityFrameworkCore.Metadata.Internal.ClrICollectionAccessor`3.Add(Object entity, Object value, Boolean forMaterialization)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.AddToCollection(INavigationBase navigationBase, InternalEntityEntry value, Boolean forMaterialization)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry.AddToCollection(INavigationBase navigationBase, InternalEntityEntry value, Boolean forMaterialization)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.AddToCollection(InternalEntityEntry entry, INavigationBase navigation, InternalEntityEntry value, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.SetReferenceOrAddToCollection(InternalEntityEntry entry, INavigationBase navigation, InternalEntityEntry value, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.ToDependentFixup(InternalEntityEntry dependentEntry, InternalEntityEntry principalEntry, IForeignKey foreignKey, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, Boolean fromQuery)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.TrackedFromQuery(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.TrackedFromQuery(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.MarkUnchangedFromQuery()
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.Query.QueryContext.StartTracking(IEntityType entityType, Object entity, ValueBuffer valueBuffer)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.<PopulateIncludeCollection>g__ProcessCurrentElementRow|60_0[TIncludingEntity,TIncludedEntity](<>c__DisplayClass60_0`2& )
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.PopulateIncludeCollection[TIncludingEntity,TIncludedEntity](Int32 collectionId, QueryContext queryContext, DbDataReader dbDataReader, SingleQueryResultCoordinator resultCoordinator, Func`3 parentIdentifier, Func`3 outerIdentifier, Func`3 selfIdentifier, IReadOnlyList`1 parentIdentifierValueComparers, IReadOnlyList`1 outerIdentifierValueComparers, IReadOnlyList`1 selfIdentifierValueComparers, Func`5 innerShaper, INavigationBase inverseNavigation, Action`2 fixup, Boolean trackingQuery)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
at Microsoft.EntityFrameworkCore.DbContext.RemoveRange(IEnumerable`1 entities)
at Castle.Proxies.Invocations.DbContext_RemoveRange_1.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()

__

我尝试将我的配置更改为此

builder
    .OwnsMany<SendingRuleLBACFilter>(
        "_lbacFilters",
        builder =>
        {
            builder.ToTable("SendingRuleLBACFilters");
            builder.WithOwner()
                .HasForeignKey("SendingRulesId");
        }).Navigation(x => x.LBACFilters);

现在它给了我

Unable to determine the relationship represented by navigation 'SendingRules.LBACFilters' of type 'IReadOnlyCollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

这是怎么回事?

我将支持字段的名称更改为

后它开始工作了
`_lBACFilters`

但这太蹩脚了。如果有人有更好的解决方案,我不必有一个愚蠢的名字我会接受你的回答