Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2259
  • Last Modified:

LINQ issues with HasLoadedOrAssignedValue

I'm having issues when I try to set a value of my ProductID fields within my Orders table that has a foriegn key relationship to the tblProduct table. When it tries to set the value, LINQ checks if tblProduct.HasLoadedOrAssignedValue = true. If so, it returns an exception and will not allow the update. See property below. How do I force LINQ to set the value? This is all occuring when I am creating a new record to write out to the table Orders.

public int ProductID
{
      get
      {
            return this._ProductID;
      }
      set
      {
            if ((this._ProductID != value))
            {
                  if (this._tblProduct.HasLoadedOrAssignedValue)
                  {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                  }
                  this.OnProductIDChanging(value);
                  this.SendPropertyChanging();
                  this._ProductID = value;
                  this.SendPropertyChanged("ProductID");
                  this.OnProductIDChanged();
            }
      }
}
0
rawcoder
Asked:
rawcoder
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
Can you describe this scenario?  Did you load the Order from the database, and are you trying to change the foreign key relationship to Product?  Something is setting the foreign key ProductID value.
0
 
Fernando SotoCommented:
Hi rawcoder;

Look at this link it should help.

LINQ to SQL associations - how to change the value of associated field
http://stackoverflow.com/questions/197753/linq-to-sql-associations-how-to-change-the-value-of-associated-field

Fernando
0
 
rawcoderAuthor Commented:
I am adding a new record to the database. So at this point I am trying to set the value of ProductID. But when I create my instance, Linx is setting it to 0 which is not a valid value. I think it is doing this because ProductID is set to Not Null in the table.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bob LearnedCommented:
"But when I create my instance..."
How are you creating your instance?  Are you creating the instance, and then setting the foreign key value?
0
 
rawcoderAuthor Commented:
Yes.
0
 
Bob LearnedCommented:
Can you show some code so that we can see how this is happening?
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now