2010-09-05 48 views
1

我不知道是否有人可以幫助我。我正在使用VS2010,.Net 4,EF4和我試圖生成一個完全在代碼中配置的objectcontext(代碼,而不是模型優先或db優先)。EF4 Builder.Configurations - 無法推斷實體類型的密鑰

總之,我做的:

  • 創建中,ContextBuilder
  • 配置中,ContextBuilder爲每個實體(指定PK,FKS,空,等)
  • 我問中,ContextBuilder創建上下文我使用提供的連接字符串。

上面的最後一步失敗,出現以下異常:

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

我認爲這意味着它不能爲我的實體推斷主鍵/標識列。

我的代碼如下。引用的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 

如果我排除部門從配置,同樣的情況下一實體等 - 因此,無論所有實體/配置是錯誤的還是它不是實體/配置問題

如果我只是Create()調用之前檢查Builder,我可以看到符合我的實體,其3個配置和指定的id領域有以下幾點:(我的評論在[] S)

Builder.Configurations.Items(0)[Sector].Items(0)[id].Value.StoreGeneratedPattern = Identity {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()調用