Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:41:31+00:00 2026-05-16T20:41:31+00:00

I wonder if someone can help me. I’m using VS2010, .Net 4, EF4 and

  • 0

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
    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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:41:32+00:00Added an answer on May 16, 2026 at 8:41 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder if someone can help me. I'm trying to add a reference to
I wonder if someone can help me. I'm trying to display a toast element
I wonder if someone can help: Long story short, I'm using MSSQL2005 to build
I wonder whether someone can help me please. I've created this form which allows
I wonder if someone can help i am trying to incorporate a live search
I wonder whether someone can help me please. I'm trying to implement the mysql_insert_id()
I wonder if someone can help me. I've got a console App which I
I wonder whether someone can help me please. I've put together this page, which
I wonder whether someone can help me please. I'm trying to put together a
I wonder whether someone can help me please. I have a mySQL database which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.