Solved

LINQ issues with HasLoadedOrAssignedValue

Posted on 2010-09-20
6
2,100 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Suggested Solutions

Title # Comments Views Activity
tempdb log contention 16 39
SQL Server 2012 r2 Make faster Temp Table 17 105
Addition to SQL for dynamic fields 6 38
migrate a SQL 2008 to 2016, 2 28
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

770 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