Solved

LINQ issues with HasLoadedOrAssignedValue

Posted on 2010-09-20
6
2,091 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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.

932 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

10 Experts available now in Live!

Get 1:1 Help Now