1

誰かが私を助けてくれるのだろうか。

私はVS2010、.Net 4、EF4を使用しており、完全にコードで構成されたオブジェクトコンテキストを生成しようとしています(モデルファーストまたはdbファーストではなくコードのみ)。

要するに、私は:

  • ContextBuilder を作成する
  • エンティティごとに ContextBuilder を構成します (PK、FK、Nullable などを指定します)。
  • 提供された接続文字列を使用して ContextBuilder にコンテキストを作成するように依頼します。

上記の最後の手順は、次の例外で失敗します。

Unable to infer a key for entity type 'MyNamespace.MyEntityName'.

これは、エンティティの主キー/ID 列を推測できないことを意味します。

私のコードは以下です。参照されるのContextExtensionは、単に から継承するラッパー クラスObjectContextです。

''Process starts with a call to this function
Private Shared Function GenerateContext() As ObjectContext
    Dim ConnectionStringDetails = System.Configuration.ConfigurationManager.ConnectionStrings("MyConnectionString")
    Dim Builder = New ContextBuilder(Of ContextExtension)()
    ConfigureDatabase(Builder)
            '' Below line throws an exception:
            '' Unable to infer a key for entity type 'MyNamespace.Sector'
    Dim NewContext = Builder.Create(New SqlConnection(ConnectionStringDetails.ConnectionString))
    Return DirectCast(NewContext, ObjectContext)
End Function

Private Shared Sub ConfigureDatabase(ByRef Builder As ContextBuilder(Of ContextExtension))
    ''Apply configurations for each of my 3 entities (see code blocks below for efinitions)
    ConfigureEntity(Builder, New SectorConfig)
    ConfigureEntity(Builder, New SolarSystemConfig)
    ConfigureEntity(Builder, New UniverseConfig)
End Sub

Private Shared Sub ConfigureEntity(Of T)(ByRef Builder As ContextBuilder(Of ContextExtension), ByVal config As EntityConfiguration(Of T))
    ''simple pluralization of the entity set
    ConfigureEntity(Builder, config, GetType(T).Name & "s")
End Sub

Private Shared Sub ConfigureEntity(Of T)(ByRef Builder As ContextBuilder(Of ContextExtension), ByVal config As EntityConfiguration(Of T), ByVal setName As String)
    ''add the configuration
    Builder.Configurations.Add(config)
    ''register the set metadata
    Builder.RegisterSet(Of T)(setName)
End Sub

私のエンティティは次のように定義されています。

Public Class Sector
    Implements IEntity
    Public Overridable Property id As Long Implements IEntity.id
    Public Overridable Property Universe As Universe
    Public Overridable Property SolarSystems As ICollection(Of SolarSystem)
End Class

そして、私のエンティティ構成は次のようになります:

Public Class SectorConfig
    Inherits EntityConfiguration(Of Sector)
    Public Sub New()
        [Property](Function(x) x.id).IsIdentity()
        Relationship(Function(x) x.Universe).FromProperty(Function(y) y.Sectors).IsRequired()
    End Sub
End Class

構成からセクターを除外すると、次のエンティティにも同じことが起こります。つまり、すべてのエンティティ/構成が間違っているか、エンティティ/構成の問題ではありません。

Builder呼び出しの直前に調べるCreate()と、エンティティに一致する 3 つの構成と、id指定されたフィールドに次のものが含まれていることがわかります: ([] 内の私のコメント)

Builder.Configurations.Items(0)[Sector].Items(0)[id].Value.StoreGeneratedPattern = Identity {1}

これは、構成が正しく適用されていることを意味しているようです。

例外が発生する理由と、問題を解決する方法を誰かが説明してもらえますか?

どうもありがとう

4

1 に答える 1

0

次の構文を使用してこれを解決できました。

Public Class SectorConfig
    Inherits EntityConfiguration(Of Sector)
    Public Sub New()
        HasKey(Function(x) X.id)
        [Property](Function(x) x.id).IsIdentity()
        Relationship(Function(x) x.Universe).FromProperty(Function(y) y.Sectors).IsRequired()
    End Sub
End Class

追加の HasKey() 呼び出しに注意してください

于 2010-09-16T14:38:48.693 に答える