I am new to C# and .NET and I just started to study LINQ to SQL, I like it. But.. I found this one very annoying thing about it. Implementing lookUps is very complex because of the “ForeignKeyReferenceAlreadyHasValueException”! There is just NO simple straight-forward way of doing it! I noticed, if I delete all associations between LINQ entities, the “ForeignKeyReferenceAlreadyHasValueException” problem is no more! I am planing to develop small WinForms database applications, with no more then 100 tables…
So my question is:
What do I lose/risk if I use LINQ2SQL but delete all the associations between LINQ entities and keep relations in database ?
Basically you’ll lose querying support, lazy-loading, IntelliSense, etc. For example, you won’t have things like this if you delete the association between Users and Questions:
The point of LINQ is to provide you with all the constructs necessary to implement a relational database model within your C# code enabling you to query that model. If you delete all the associations/relationships LINQ to SQL loses it’s purpose.
Regarding the Exception you got:
This is what you need to do to solve your problem:
LINQ to SQL ForeignKeyReferenceAlreadyHasValueException error
For LookUps and LINQ, do this:
LINQ, Lookups, ForeignKeyReferenceAlreadyHasValueException
Use this code for example when binding a combobox: