Çözüm:
cover.OriginalSongId = song.SongId;
yerine,
cover.OriginalSong = song;
http://dotnetslackers.com/Community/blogs/bmains/archive/2009/05/21/linq-to-sql-error-foreignkeyreferencealreadyhasvalueexception.aspx
http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/744b2c6a-ecb8-44bf-b72a-dc1b85b34342
If the association property is assigned a value you cannot change the foreign key field anymore, you must change the relationship by changing the assocition property. For example, using Customer and Order from Northwind sample database. If the 'Customer' property on an order is assigned a value you can no longer manually change the order's CustomerID field, since it must match the PK of the customer. You can, however, change the 'Customer' property directly, assigning it a new customer instance. This action will automatically update the CustomerID field to match.
ForeignKeyReferenceAlreadyHasValueException
Etiketler: exception , ForeignKeyReferenceAlreadyHasValueException , linq to sql
System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext
System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported.
You may have gotten this error when using LINQ to SQL. I personally hate this error, but surprisingly enough, out of 2 years of using LINQ to SQL, this is the first time I've gotten this (2 years later, I mean). This error happens in this scenario: imagine loading up an object Order. This object has a primary key relationship Customer, which you can access via:
Order.Customer
Under the scenes, the order class uses the EntityRef
order.Customer
This reference gets loaded from the context, using the value from CustomerKey. if you assign a new value to Order.CustomerKey as in:
order.CustomerKey = 3;
The LINQ entity notices that the Customer object (underlying EntityRef
order.Customer = this.DataContext.Customers.First(i => i.Key == 1);
You have to assign the reference to the Customer, and this instead updates the key, the reverse way around. I don't like this because this forces me to perform an additional data query, and in a business app this may reduce performance just to update a reference. I chose an alternate way. By adding a partial class for the order, I add a ClearCustomer method that does the following (note all LINQ types implement partial keyword, which means you can create another partial class to add additional methods).
public partial class Order ...
{
public void ClearCustomer()
{
_Customer = default(EntityRef
}
}
And now, calling this method before changing the assignment clears the customer, and I can make the change. A bit of a hack, but it works. Unfortunately, because again the EntityRef is internal to the class, the method has to be in the class definition (can't use a static helper class to do this, at least easily). To update the reference, I can now do:
order.ClearCustomer();
order.CustomerKey = 7;
http://dotnetslackers.com/Community/blogs/bmains/archive/2009/05/21/linq-to-sql-error-foreignkeyreferencealreadyhasvalueexception.aspx
Etiketler: ForeignKeyReferenceAlreadyHasValueException , insertonsubmit , linq