“没有为属性指定的映射”,在EF

时间:2016-04-08 16:34:31

标签: entity-framework

我有一个DbContext,其中包含一个包含EF复杂类型的类的DbSet,后者又包含一个只读属性。运行我的应用程序时出现以下错误:

error 3004: Problem in mapping fragments starting at line 6:
No mapping specified for properties Frobnitzer.ComplexPropIdWrap in Set Frobnitzers.
An Entity with Key (PK) will not round-trip when:
Entity is type [ef_complex_pk.Frobnitzer]

究竟是什么问题。在https://github.com/timabell/ef-complex-pk/tree/fragment-error处可用的最小测试用例 - 运行唯一的单元测试以获得错误。

顺便提一下,这是一个非常简化的测试用例,所以对你来说可能是显而易见的,但是当它与一个具有各种流畅配置的大型项目混合在一起时,它肯定是不可能的。

完整错误:

System.Data.Entity.Infrastructure.DbUpdateException was unhandled by user code
  HResult=-2146233087
  Message=Error retrieving values from ObjectStateEntry. See inner exception for details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at ef_complex_pk.TestRunner.RunIt() in C:\repo\ef-complex-pk\ef-complex-pk\TestRunner.cs:line 18
  InnerException:
       HResult=-2146233087
       Message=Error retrieving values from ObjectStateEntry. See inner exception for details.
       Source=EntityFramework
       StackTrace:
            at System.Data.Entity.Core.Mapping.Update.Internal.RecordConverter.ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, Boolean useCurrentValues, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
            at System.Data.Entity.Core.Mapping.Update.Internal.ExtractedStateEntry..ctor(UpdateTranslator translator, IEntityStateEntry stateEntry)
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.LoadStateEntry(IEntityStateEntry stateEntry)
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.PullModifiedEntriesFromStateManager()
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
            at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
            at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
            at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
            at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
            at System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
            at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
            at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
            at System.Data.Entity.Internal.InternalContext.SaveChanges()
       InnerException:
            HResult=-2146232032
            Message=
(6,10) : error 3004: Problem in mapping fragments starting at line 6:No mapping specified for properties Frobnitzer.ComplexPropIdWrap in Set Frobnitzers.
An Entity with Key (PK) will not round-trip when:
  Entity is type [ef_complex_pk.Frobnitzer]

            Source=EntityFramework
            StackTrace:
                 at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGenerateViews(EntityContainerMapping entityContainerMap, Dictionary`2 resultDictionary)
                 at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.ViewDictionary.SerializedGetGeneratedViews(EntityContainer container)
                 at System.Data.Entity.Core.Common.Utils.Memoizer`2.<>c__DisplayClass2.<Evaluate>b__0()
                 at System.Data.Entity.Core.Common.Utils.Memoizer`2.Result.GetValue()
                 at System.Data.Entity.Core.Common.Utils.Memoizer`2.Evaluate(TArg arg)
                 at System.Data.Entity.Core.Mapping.StorageMappingItemCollection.ViewDictionary.GetGeneratedView(EntitySetBase extent, MetadataWorkspace workspace, StorageMappingItemCollection storageMappingItemCollection)
                 at System.Data.Entity.Core.Mapping.Update.Internal.ViewLoader.InitializeEntitySet(EntitySetBase entitySetBase, MetadataWorkspace workspace)
                 at System.Data.Entity.Core.Mapping.Update.Internal.ViewLoader.SyncInitializeEntitySet[TArg,TResult](EntitySetBase entitySetBase, MetadataWorkspace workspace, Func`2 evaluate, TArg arg)
                 at System.Data.Entity.Core.Mapping.Update.Internal.ExtractorMetadata..ctor(EntitySetBase entitySetBase, StructuralType type, UpdateTranslator translator)
                 at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.GetExtractorMetadata(EntitySetBase entitySetBase, StructuralType type)
                 at System.Data.Entity.Core.Mapping.Update.Internal.ExtractorMetadata.ExtractResultFromRecord(IEntityStateEntry stateEntry, Boolean isModified, IExtendedDataRecord record, Boolean useCurrentValues, UpdateTranslator translator, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
                 at System.Data.Entity.Core.Mapping.Update.Internal.RecordConverter.ConvertStateEntryToPropagatorResult(IEntityStateEntry stateEntry, Boolean useCurrentValues, ModifiedPropertiesBehavior modifiedPropertiesBehavior)
            InnerException:

1 个答案:

答案 0 :(得分:0)

复杂类型中的属性需要至少具有受保护的setter,以便EF可以加载该值。

您可以在https://github.com/timabell/ef-complex-pk/tree/fixed-fragment-error

找到固定的示例

错误的原因并不完全明显,因为它没有提到它绊倒的实际属性,只是提到包含实体中的属性。