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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:51:05+00:00 2026-06-14T14:51:05+00:00

I have a class public partial class Advertisement { public int AdvertisementId { get;

  • 0

I have a class

public partial class Advertisement
{
    public int AdvertisementId { get; set; }
    public string Description { get; set; }
    public City CityIdFrom { get; set; }
    public City CityIdTo { get; set; }
    public int Weight { get; set; }
}

which represents the table Advertisements. Same goes for a class City

public class City
{
    public Int32 CityId { get; set; }
    public string Name { get; set; }
}

Now I have a view controller named View1Controller which has

DbConnection db = new DbConnection();

public ActionResult Index()
{
    var query = from item in db.Advertisements.AsEnumerable() select item;

    return View(query);
}

And finally there is a View1.cshtml file

@model IEnumerable<MvcApplication1.Models.Advertisement>

@{
    ViewBag.Title = "View1";
}

<h2>View1</h2>

<table>
@foreach(var item in Model)
{
    <tr>
        <td>@item.Description</td>
        <td>@item.CityIdFrom</td>
    </tr>
}
</table>

I took a look in SQL Profiler and the query generated looked

SELECT 
[Extent1].[AdvertisementId] AS [AdvertisementId], 
[Extent1].[Description] AS [Description], 
[Extent1].[Weight] AS [Weight], 
[Extent1].[CityIdFrom_CityId] AS [CityIdFrom_CityId], 
[Extent1].[CityIdTo_CityId] AS [CityIdTo_CityId]
FROM [dbo].[Advertisements] AS [Extent1]

And executing the query I get:

2   None    5000    1   2
3   Test    1000    3   4

However, when query is hit, both CityIdFrom and CityIdTo are null for some reason. Thus the result table looks

None 
Test 

Instead of expected

None 1
Test 3

What am I doing wrong?

  • 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-06-14T14:51:06+00:00Added an answer on June 14, 2026 at 2:51 pm

    you either need to add an Include for CityFrom and CityTo, or you Can make these referenced entities virtual. Option 1 (Include) avoids the select n+1 issue so commomn among ORMs.

    var query = db.Advertisements.Include("CityIdFrom").Include("CityIdTo").AsEnumerable();
    return View(query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this user defined function. public partial class UserDefinedFunctions { static int i;
I have a class : public class Car { public string Name { get;
On server I have the following class public class A { string a1 {get;
I have this class: public partial class Window1 : Window { public String Name2;
I have this class as parent class: public partial class GetStuffResult { private int
For example I have two entities Class A { public Guid Id {get;set;} public
suppose I have a class like class ABC { int ID; public string Name;
Lets have a class: public partial class MyControl: UserControl{ private ObservableCollection<string> names = new
I have a constructor question for C#. I have this class: public partial class
Suppose I have an entity object defined as public partial class Article { public

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.