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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:15:14+00:00 2026-06-08T02:15:14+00:00

I have the following two models [DataContract] public class Event { [Key] public int

  • 0

I have the following two models

[DataContract]
public class Event
{       
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public Place Place { get; set; }
}

[DataContract]
public class Place
{
    [Key]
    public string ID { get; set; }
    public string Name { get; set; }
}

and data context class:

public class myDB: DbContext
{
    public DbSet<Event> Events { get; set; }
    public DbSet<Place> Places { get; set; }
}

I run the following code for example

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        Foo();
    }

    private void Foo()
    {
        var db = new myDB();

        var place = new Place
                        {
                            ID = "{59E0F90C-03DB-453C-8C9E-4747957ED37E}",
                            Name = "Home",
                        };

        db.Places.Add(place);
        db.SaveChanges();

        var @event = new Event
        {
            Name = "Test",
            Place = place,
            TimeFrame = new TimeFrame() {StartTime = DateTime.Now, EndTime = DateTime.Now }
        };
        db.Events.Add(@event);
        db.SaveChanges();
        var eventIndex = @event.ID;

        var ev = db.Events.Find(@eventIndex);
        var evPlace = ev.Place;

        var pl = db.Places.Find("{59E0F90C-03DB-453C-8C9E-4747957ED37E}");
    }

everything works fine and evPlace is the object I just added to db, pl is also not null.

But when I try to run the following code from the controller where 5 is the ID of the new event object

var ev = _db.Events.Find(5);
var evPlace = ev.Place;

var pl = _db.Places.Find("{59E0F90C-03DB-453C-8C9E-4747957ED37E}");

pl and ev are not null but ev.Place is null (evPlace)

Any idea why?

  • 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-08T02:15:16+00:00Added an answer on June 8, 2026 at 2:15 am

    The problem has nothing to do with the primary key being a string or not. Generally Entity Framework doesn’t load navigation properties automatically. You need to specify that explicitly:

    var ev = db.Events.Include(e => e.Place).SingleOrDefault(e => e.ID == 5);
    

    Or: If you declare the Place property as virtual…

    public virtual Place Place { get; set; }
    

    …EF will enable lazy loading for this property and load the Place from the database as soon as you access it in your code. Your code example would work then. Be aware that lazy loading leads to a second query/roundtrip to the database whereas eager loading (Include) loads both the event and the place in a single query.

    More about loading related entities is here.

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

Sidebar

Related Questions

I have the following two models class Author(Model): name = CharField() class Publication(Model): title
I have following two models class Questionnaire(models.model) name = models.CharField(max_length=128, null=True, blank=True) type =
I have the following two models: class Parent < ActiveRecord::Base has_one :child, dependent: :destroy
I have the following two models: class Provider(models.Model): provider = models.CharField(max_length=100, unique=True) class UserProfile(models.Model):
I am little bit stuck with the following problem. I have two models: class
I have the following two models class ContactField < ActiveRecord::Base end class Address <
I have two models class Status(models.Model) name = models.CharField(max_length=25) rank = models.PositiveSmallIntegerField() class MyModel(models.Model)
So imagine you have the following two tables: CREATE movies ( id int, name
The scenario Suppose I have the following two model classes: public class ProductColor {
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user

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.