LIH
LIH

Reputation: 933

Code First select data - Related entities always returns null

I am trying to implement a system using Entity Framework. These are two classes I have

Class One

public class ConsumableGood
{
    public ConsumableGood() { }

    public Guid ConsumableGoodId { get; set; }
    public string ConsumableGoodName { get; set; }
    public string ConsumableGoodPrice { get; set; }
    public virtual Category Category { get; set; }

    public ICollection<ConsumableGoodsStock> ConsumableGoodsStocks { get; set; }

}

Class Two

public class ConsumableGoodsStock
{
    public ConsumableGoodsStock()
    {

    }

    public Guid ConsumableGoodsStockId { get; set; }
    public double ConsumableGoodPriceIn { get; set; }
    public double ConsumableGoodPriceOut { get; set; }
    public int ConsumableGoodQuantity { get; set; }
    public ConsumableGood ConsumableGood { get; set; }

    public ICollection<OrderItem> OrderItems { get; set; }
}

I am trying to select records from ConsumableGoodsStock. But the problem is every time ConsumableGood returns null though it has a value. This is the code I'm trying to use.

DataTable table = new DataTable();
table.Columns.Add("Consumable Good");
table.Columns.Add("Price In");
table.Columns.Add("Price Out");
table.Columns.Add("Quantity");

var ConsumableGoodsStocks = from db in em.ConsumableGoodsStocks select db;

foreach (var consumableGoodStock in ConsumableGoodsStocks)
{
    DataRow row = table.NewRow();
    ConsumableGood consumableGood = consumableGoodStock.ConsumableGood;
    row[0] = consumableGood.ConsumableGoodName.ToString();
    row[1] = consumableGoodStock.ConsumableGoodPriceIn.ToString();
    row[2] = consumableGoodStock.ConsumableGoodPriceOut.ToString();
    row[3] = consumableGoodStock.ConsumableGoodQuantity.ToString();
    table.Rows.Add(row);
}
return table;

consumableGood is always returning null. Thanks in advance.

Upvotes: 4

Views: 2325

Answers (1)

ocuenca
ocuenca

Reputation: 39376

I think what you looking for is lazy load the related entities. To do that you need to specify your navigation properties as virtual. From this page:

Lazy loading is the process whereby an entity or collection of entities is automatically loaded from the database the first time that a property referring to the entity/entities is accessed. When using POCO entity types, lazy loading is achieved by creating instances of derived proxy types and then overriding virtual properties to add the loading hook.

When you specify the virtual keyword in your navigation properties, EF creates at runtime dynamic proxies for your entity classes. Those proxy classes are responsible for the lazy loading behavior of the related entities. Without virtual, lazy loading will not be supported, and you get null on your navigation properties. So you need to do this:

public class ConsumableGoodsStock
{
    //...
    public virtual ConsumableGood ConsumableGood { get; set; }

    public virtual  ICollection<OrderItem> OrderItems { get; set; }
{

In this link you will find all the requiriments you need to follow to support lazy loading.

Upvotes: 7

Related Questions