C# Entity Framework Property is (Sometimes) Null

.net c# entity-framework entity-framework-6 winforms

Question

Therefore, I am seeing a very strange problem with EF where a class's property is sometimes null and other times it is not. When it should have a value, ParentType in this situation is frequently null.

The code in question 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, when I use this line of code to retrieve the same object from a listbox, its ParentType changes to null.

Here is a diagram of how my classes and DB 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"));

}
1
3
1/31/2016 6:49:24 PM

Accepted Answer

I believe that in your first code snippetParentMatterType is also null. Let's slightly alter it:

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 look,types You can observe that there aren't any errors in the debugger.ParentMatterType is present. This is due to the property not beingvirtual thus it is not loaded carelessly (which would happen if you inspect it in the debugger).

even if you improve the housevirtual The context is immediately discarded, so you won't be able to read its information later (which is good, by the way). If you were to attempt to obtainParentMatterType After that, EF would raise an error.

Instead, you ought toInclude By altering the initial section of your query, the property:

var types = context.MatterTypes.Include(m => m.ParentMatterType)
...
2
1/22/2016 10:03:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow