6

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); } } 
3
  • This is strange. Can you show how are you retrieving SettingCategory? Commented Nov 2, 2012 at 17:32
  • Are you sure you have related Settings in your database? Commented Nov 2, 2012 at 17:33
  • I am using Code first with migrations so it should make everything right? I added my Initial script. Commented Nov 2, 2012 at 17:40

1 Answer 1

16

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); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that, but the problem is that s.Settings is not a recognized expression! Cannot resolve symbol 'Settings' That's why I am scratching my head.
I tried this now: .Include("Settings") and that worked, strange that the lambda isn't working.
Have you added a reference (using) to System.Data.Entity? The strongly-typed Include is highly recommended when doing refactoring.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.