开发者

EF4 Builder.Configurations - Unable to infer a key for entity type

I wonder if someone can help me.

I'm using VS2010, .Net 4, EF4 and I'm trying to generate an objectcontext which is configured entirely in code (code-only as opposed to model-first or db-first).

In short, I do:

  • Create a ContextBuilder
  • Configure ContextBuilder for each entity (specifying PK, FKs, Nullable, etc.)
  • I ask the ContextBuilder to create a context for me using the provided connection string.

The last step above fails with the following exception:

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

I take this to mean it can't infer a Primary Key/Identity column for my entity.

My code is below. The ContextExtension referenced is a wrapper class which simply inherits from ObjectContext.

''Process starts with a call to this function
Private Shared Function GenerateContext() As ObjectContext
    D开发者_开发技巧im 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

My entities are defined as below:

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

And my entity configurations are like this:

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

If I exclude sector from the config, the same happens to the next entity an so on - so either all entities/configs are wrong or it's not an entity/config problem

If I examine Builder just before the Create() call, I can see 3 configurations which match my entities and the id fields specified have the following: (My comments in []s)

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

Which seems to imply that the configuration is being applied correctly.

Can someone please explain why I'm getting the exception and how to go about resolving the problem?

Many thanks


I was able to resolve this by using the following syntax:

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

Note the additional HasKey() call

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜