So I am having a very weird issue when using EF where sometimes property of a class is null and sometimes it is not. In this case ParentType is often null, when it should have a value
With the following code is DOES have a value:
using (Context context = new Context())
{
checkedListBox_SubTypes.DataSource = context.Types.Where(x => x.ParentType != null && x.ParentType.TypeID == _selectedType.TypeID).ToList();
}
However with this line of code, where i try to get the same object back out of a listbox, it ParentType becomes null
Below are my classes and DB diagram for how things are set up:
public class Type
{
#region Fields
#endregion Fields
#region Constructor
public Type()
{
}
#endregion Constructor
#region Properties
public int? TypeID { get; set; }
[DisplayName("Type")]
public string TypeName { get; set; }
public Type ParentType { get; set; }
/// <summary>
/// List of Types this Type is associated with (Parent / Sub)
/// </summary>
public virtual ICollection<Type> Types { get; set; }
public virtual ICollection<Object> Objects { get; set; }
#endregion Properties
Context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Set Many-To-Many relationship Mapping between Object & Type
modelBuilder.Entity<ObjectType>()
.HasMany(x => x.Objects)
.WithMany(x => x.Types)
.Map(m => m.ToTable("ObjectType")
.MapLeftKey("TypeID")
.MapRightKey("ObjectNumber"));
//Set One-To-Many relationship Mapping between Type &(Parent)Type
modelBuilder.Entity<Type>()
.HasMany(x => x.Types)
.WithOptional(x => x.ParentType)
.Map(m => m.ToTable("Type")
.MapKey("ParentTypeID"));
}
I'm pretty sure that in your first code fragment ParentMatterType
is null too. Let's change it a little bit:
using (LitTrackContext context = new LitTrackContext())
{
var types = context.MatterTypes
.Where(x => x.ParentMatterType != null
&& x.ParentMatterType.MatterTypeID == _selectedMatterType.MatterTypeID)
.ToList();
checkedListBox_MatterSubTypes.DataSource = types;
}
If you inspect types
in the debugger you'll see that there are no ParentMatterType
s in there. That's because the property isn't virtual
, so it isn't loaded lazily (which would happen if you inspect it in the debugger).
Even if you make the property virtual
you won't be able to see its content later, because the context is disposed immediately (which is good, by the way). If you would try to access ParentMatterType
afterwards, EF would throw an exception.
You should instead Include
the property in your query, by changing the first part:
var types = context.MatterTypes.Include(m => m.ParentMatterType)
...