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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:00:16+00:00 2026-06-14T03:00:16+00:00

I have 2 simple classes: public class Setting { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid SettingId

  • 0

I have 2 simple classes:

public class Setting
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid SettingId { get; set; }

    [Required]
    public String Name { get; set; }

    public String Value { get; set; }

    [Required]
    public SettingCategory SettingCategory { get; set; }
}

public class SettingCategory
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid SettingCategoryId { get; set; }

    [Required]
    public String Value { get; set; }

    public ICollection<Setting> Settings { get; set; }
}

When I retrieve a SettingCategory from the database the collection Settings is always null.

When I make it a virtual then it will say: The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.

How can I access my Settings list?

The other way around works, if I retrieve a Setting from the database the SettingCategory property is filled.

This is my initial code-migrations script:

CreateTable(
    "dbo.Settings",
    c => new
        {
            SettingId = c.Guid(nullable: false, identity: true),
            Name = c.String(nullable: false),
            Value = c.String(),
            SettingCategory_SettingCategoryId = c.Guid(nullable: false),
        })
    .PrimaryKey(t => t.SettingId)
    .ForeignKey("dbo.SettingCategories", t => t.SettingCategory_SettingCategoryId, cascadeDelete: true)
    .Index(t => t.SettingCategory_SettingCategoryId);

CreateTable(
    "dbo.SettingCategories",
    c => new
        {
            SettingCategoryId = c.Guid(nullable: false, identity: true),
            Value = c.String(nullable: false),
        })
    .PrimaryKey(t => t.SettingCategoryId);

And this is the part that gets it from the database:

public SettingCategory Get(Guid settingCategoryId)
{
    using (var context = new BackofficeContext())
    {
        return context
            .SettingCategories
            .FirstOrDefault(s => s.SettingCategoryId == settingCategoryId);
    }
}

Answer

I forgot the include in .SettingCategories, but I was trying it with a lambda:

public SettingCategory Get(Guid settingCategoryId)
{
    using (var context = new BackofficeContext())
    {
        return context
            .SettingCategories
            .Include(s => s.Settings)
            .FirstOrDefault(s => s.SettingCategoryId == settingCategoryId);
    }
}

That doesn’t work, but this does:

public SettingCategory Get(Guid settingCategoryId)
{
    using (var context = new BackofficeContext())
    {
        return context
            .SettingCategories
            .Include("Settings")
            .FirstOrDefault(s => s.SettingCategoryId == settingCategoryId);
    }
}
  • 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-14T03:00:17+00:00Added an answer on June 14, 2026 at 3:00 am

    Because you are disposing of your BackofficeContext you cannot use LazyLoading, which is what is happening when you make Settings virtual.

    You can either increase the lifetime of your BackofficeContext, or eager load Settings. You can use eager loading with Include.

    public SettingCategory Get(Guid settingCategoryId)
    {
        using (var context = new BackofficeContext())
        {
            return context
                .SettingCategories
                .Include(s => s.Settings)
                .FirstOrDefault(s => s.SettingCategoryId == settingCategoryId);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple classes: public class Order { public int Id {get;set;} public IList<Name>
I have a simple Fluent NHibernate model with two related classes: public class Applicant
This is simple version of my problem. I have 3 classes: public class TopographyFrame
I have two simple classes public class Blog { public Blog(){ Comments=new List<Comment>(); }
I have two simple classes generated by code first. public class Company { public
I have a very simple composite model made of two classes: Public Class ParentModelVM
I have two simple Model classes and a ViewModel... public class GridItem { public
I have a problem with these simple classes: public class Thread { public string
I have the following simple textbook classes defined: public class Product { public int
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True)

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.