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 7186487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:38:28+00:00 2026-05-28T18:38:28+00:00

I have a couple of classes (for this example anyway) that use code first

  • 0

I have a couple of classes (for this example anyway) that use code first with the entity framework to connect to the database.

public class Customer
{
    [Key]
    public long CustomerId { get; set; }
    public string CompanyName { get; set; }
    ...
    public virtual List<Contact> Contacts { get; set; }
}

public class Contact
{
    [Key]
    public long ContactId { get; set; }
    public string Forename { get; set; }
    ...
    public long CustomerId { get; set; }

    public virtual Customer Customer { get; set; }
}

When I hook these up in my context class directly to the db the foreign key relationships hook up fine and I can access the collection of contacts from within the customer class.

class RemoteServerContext : DbContext 
{
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Contact> Contacts { get; set; }
    ...
}

My problem is that these database tables are used by various different systems and are massive. In order to increase efficiency I have overridden the default behaviour to point at a view (and also a stored proc elsewhere) rather than directly at the table.

    public IEnumerable<Customer> Customers ()
    {
        return Database.SqlQuery<Customer>("SELECT * FROM vw_CustomerList");
    }

    public IEnumerable<Contact> Contacts()
    {
        return Database.SqlQuery<Contact>("SELECT * FROM vw_ContactsList");
    }

I have made sure that in each of the views I have included the foreign key fields: CustomerId and ContactId.

When I do this however the class joins appear to be lost – there’s always a null when I drill into either of the objects where it should be pointing to the other one. I have tried to set up what the foreign key field should point to but this doesn’t seem to help either.

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Contact>().HasRequired(p => p.Customer)
            .WithMany()
            .HasForeignKey(k => k.CustomerId);
    }

Is there a way to establish the connection when overriding the default behaviour?

  • 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-28T18:38:29+00:00Added an answer on May 28, 2026 at 6:38 pm

    There is no overriding in this case. If you removed

    public DbSet<Customer> Customers { get; set; }
    

    and replaced it with

    public IEnumerable<Customer> Customers ()
    {
        return Database.SqlQuery<Customer>("SELECT * FROM vw_CustomerList");
    }
    

    you have completely changed the behavior. The first uses entities and full power of EF. The second is only helper to execute custom SQL. Second without first or without defining entity in OnModelCreating doesn’t use Customer as mapped entity at all – it uses it as any normal class (only mapped entities can use features like lazy loading).

    Because your Customer is now mapped to view you cannot use your former Customer class used with table. You must define mapping of Customer to a view by cheating EF:

    modelBuilder.Entity<Customer>().ToTable("vw_ContactsList"); // EF code fist has no view mapping
    

    Once you have this you can try again using:

    public DbSet<Customer> Customers { get; set; }
    

    Unless your view is updatable you will get exception each time you try to add, update or delete any customer in this set. After mapping relation between Customer and Contact mapped to views your navigation properties should hopefully work.

    The problem with SqlQuery is the way how it works. It returns detached entities. Detached entities are not connected to the context and they will not lazy load its navigation properties. You must manually attach each Customer instance back to context and to do that you again need DbSet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok I have couple of .NET classes that I want to use in VBA.
I have a couple classes that have nearly identical code. Only a string or
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
We have a couple of very very slow JUnit tests that make heavy use
This is a convoluted example so bear with me: I have a script that
I'm find I have several places that having public static inner classes designed that
I have a couple of classes that want to pass each other some information
I have a couple of packages I have trouble naming. This package contains classes
I have a couple classes that can each have comments: class Movie < ActiveRecord::Base
I have a couple of classes that I have created for my web project

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.