Solved

LINQ issues with HasLoadedOrAssignedValue

Posted on 2010-09-20
6
2,073 Views
Last Modified: 2012-05-10
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
Comment
Question by:rawcoder
  • 3
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33725042
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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33729333
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
 

Author Comment

by:rawcoder
ID: 33740366
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 33743335
"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
 

Author Comment

by:rawcoder
ID: 33750493
Yes.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 33753342
Can you show some code so that we can see how this is happening?
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now