DbContext template question

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
PatrickG
Posts: 12
Joined: Thu 10 Mar 2011 21:51

DbContext template question

Post by PatrickG » Fri 14 Dec 2012 23:59

I noticed an unexpected behaviour that I'm not sure if is by design or if it's a bug.

When I generate Entities using the POCO template I get objects based on ObjectContext. The generated entities have navigation properties that follow the pattern of:

Code: Select all

/// <summary>
/// There are no comments for MyItems in the schema.
/// </summary>
public virtual ICollection<MyItem> MyItems
{
    get
    {
      if (_MyItems == null)
      {
	  var newCollection = new FixupCollection<MyItem>();
	  newCollection.CollectionChanged += FixupMyItems;
	  _MyItems = newCollection;
      }
      return _MyItems;
    }
    set
    {
      if (!ReferenceEquals(_MyItems, value))
      {
	  var previousValue = _MyItems as FixupCollection<MyItem>;
	  if (previousValue != null)
	  {
	    previousValue.CollectionChanged -= FixupMyItems;
	  }
	  _MyItems = value;
	  var newValue = value as FixupCollection<MyItem>;
	  if (newValue != null)
	  {
	      newValue.CollectionChanged += FixupMyItems;
	  }
      }
    }
}
private ICollection<MyItem> _MyItems;

private void FixupMyItems(object sender, NotifyCollectionChangedEventArgs e)
{
  if (e.NewItems != null)
  {
      foreach (MyItem item in e.NewItems)
      {
	  item.ProductVersion = this;
      }
  }

  if (e.OldItems != null)
  {
      foreach (MyItem item in e.OldItems)
      {
	  if (ReferenceEquals(item.MyParent, this))
	  {
	      item.MyParent = null;
	  }
      }
  }
}

But if I use the DbContext template, then the navigation properties have a pattern of:

Code: Select all

/// <summary>
/// There are no comments for MyItems in the schema.
/// </summary>
public virtual ICollection<MyItem> MyItems
{
    get;
    set;
}      
   
The key for me is that if I have code such as:

Code: Select all

MyParent p = new MyParent() { Name = "Some Name" };
MyDbContext.Parents.Add(p);
p.MyItems.Add(new MyItem() { Quantity = 8} );
I get a null reference exception where I wouldn't have using the POCO template. Is this the intention?

Thanks,

Patrick

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: DbContext template question

Post by Shalex » Fri 21 Dec 2012 13:59

We have reproduced the issue with navigation properties when using the DbContext template. We will investigate it and notify you about the results as soon as possible.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: DbContext template question

Post by Shalex » Wed 26 Dec 2012 17:26

There are some differences in usage between DbContext and POCO templates.
If you want to employ the navigation properties for talking to objects, which are created by user (not read from database), when using dynamic proxy for classes in DbContext, it is necessary to create instances of the corresponding classes in a different way:

Code: Select all

    //creating dynamic proxy class which is descendant of MyParent
    MyParent p = MyDbContext.Parents.Create();
    p.Name = "Some Name";
    MyDbContext.Parents.Add(p);
    p.MyItems.Add(new MyItem() { Quantity = 8} );

Post Reply