4

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.

1
  • Possible put your DbContext here.
    – SelvaS
    Mar 16, 2015 at 15:07

1 Answer 1

7

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.

1
  • 1
    Good to know ;), If you think my answer solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
    – ocuenca
    Mar 16, 2015 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.