This is a migrated thread and some comments may be shown as answers.

ForeignKeyReferenceAlreadyHasValueException

2 Answers 39 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sindhupriya
Top achievements
Rank 1
Sindhupriya asked on 04 Apr 2016, 09:37 AM

Hi,

I am getting this exception and need help badly.

Scenario:

I have a table Users. Binded this entity to GridView.

I have one more table Roles. Id of Roles is foreignKey RoleID in Users.

Binded this as a combo-box column in GridView

this.radGridView.Columns.Add(new GridViewComboBoxColumn()
                            {
                                ItemsSourceBinding = new System.Windows.Data.Binding("Roles") { Source = this.DataContext },
                                DataMemberBinding = new System.Windows.Data.Binding("Role") ,
                                DisplayMemberPath = "Role",
                                SelectedValueMemberPath = "RoleID",
                                Header = "User-Role",
                                FilterMemberPath="RoleReconUserRoles.Role",
                                IsReadOnly=false,
                                Name = "gridView"
                            });

It works fine. But, when I change the item in the combo-box, and try to commit, it changes back to the original value because I get this ForeignKeyReferenceAlreadyHasValueException exception in the entity.

/// <summary>
        /// Gets or sets the Role column value.
        /// </summary>
        [System.Data.Linq.Mapping.Column(Name = "Role", Storage = "_role", DbType = "int NOT NULL", IsPrimaryKey = true, CanBeNull = false, UpdateCheck = System.Data.Linq.Mapping.UpdateCheck.Never)]
        [System.Runtime.Serialization.DataMember(Order = 3)]
        [System.CodeDom.Compiler.GeneratedCode("CodeSmith", "6.0.0.0")]
        public int Role
        {
            get { return _role; }
            set
            {
                if (_role != value)
                {
                    if (_roleReconUserRoles.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    OnRoleChanging(value);
                    SendPropertyChanging("Role");
                    _role = value;
                    SendPropertyChanged("Role");
                    OnRoleChanged();
                }
            }
        }

 

Any help is appreciated!

Thanks in advance,

Sindhu

2 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 07 Apr 2016, 06:50 AM
Hi Sindhu,

I believe that this thread will be helpful for your case.

Regards,
Yoan
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Sindhupriya
Top achievements
Rank 1
answered on 07 Apr 2016, 07:09 AM

Hi Yoan,

That did not help me fix the issue.

Instead, I changed the combo-box column addition to the grid from code to XAML and it is working as expected now.

 

Thanks anyway,

Sindhu

Tags
GridView
Asked by
Sindhupriya
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Sindhupriya
Top achievements
Rank 1
Share this question
or