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

  • Home
  • SEARCH
  • 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 5974751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:59:10+00:00 2026-05-22T20:59:10+00:00

I am using Entity Framwork DbContexts with a legacy database. I have 2 different

  • 0

I am using Entity Framwork DbContexts with a legacy database. I have 2 different properties of an entity that both need to reference the same lookup table, like so:


public class Address
{
   public virtual AddressType AnAddressType {get; set;}
   public virtual AddressType AnotherAddressType {get; set;}
}

// now here's a LINQ query that just flat won't work:
from a in Addresses select a;


The exception indicates that we tried to include a completely fictional field in the select list — the field doesn’t appear in my POCO or the table — it looks like it was expected by convention, it’s named AnAddressType_AddressType or something close to that

The AddressType entity does not have a corresponding navigation property on it. I cannot seem to get this to work. When I attempt to select data with my LINQ query, I get runtime errors.

Edit

I have other relations that are working (this code is generated from the “stock” DbContext generator). The thing about this one relation that is different is that the lookup table does not have a navigation property back to the main table (the lookup table is used all over the place, so I don’t really want to add nav properties from it to everything that uses it). EF seems to be having a problem with that. It’s probably a configuration vs. convention thing, and I have inadvertently tripped on some kind of convention problem.

  • 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-22T20:59:10+00:00Added an answer on May 22, 2026 at 8:59 pm

    You have a foreign key column name in your legacy database which doesn’t follow the EF conventions, for example: The foreign key column in your Addresses table to the AddressTypes table for the AnAddressType relationship has the name MyCrazyAnAddressTypeNumberCodeKeyId.

    But EF will assume by convention that the FK column name is: [Nav.property]_[KeyColumn]. For example: If AddressType has a PK with name AddressTypeId EF will assume the FK column has the name AnAddressType_AddressTypeId. Because this doesn’t match you get the exception you describe. You must specify the FK column name explicitely to fix this problem:

    modelBuilder.Entity<Address>()
                .HasRequired(a => a.AnAddressType)
                .WithMany()
                .Map(c => c.MapKey("MyCrazyAnAddressTypeNumberCodeKeyId"))
                .WillCascadeOnDelete(false);
    

    (code snippet partially stolen from Ladislav’s answer for convenience)

    That’s my hypothesis.

    Edit

    Alternatively you can introduce a foreign key property into your model and tell EF by data annotations that this property is a FK to the corresponding navigation property:

    public class Address
    {
       [ForeignKey("AnAddressType")]
       public int MyCrazyAnAddressTypeNumberCodeKeyId {get; set;}
       public virtual AddressType AnAddressType {get; set;}
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say you create an object and saving to database by using ADO Entity Framwork
Using Entity Framework with MVC2, I have a series of date textboxes, that I
I'm using entity framework 4. I have a stored procedure that just updates one
I am using Entity Framework with my website. To improve performance, I have started
I have a problem with the following Linq query using Entity Framework: from o
So, I am using the Linq entity framework. I have 2 entities: Content and
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
Using Entity Framework I have the fields: o CreatedOn (datetime) o CreatedBy (nvarchar(50)) o
I am using DTOs and converters in an entity framwork scenario. I want to
I am using entity framework with a web service and I have entity partial

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.