Solved

LINQ issues with HasLoadedOrAssignedValue

Posted on 2010-09-20
6
2,122 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 63

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
Technology Partners: 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!

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

730 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