ForeignKeyReferenceAlreadyHasValueException

3 posts, 0 answers
  1. Sindhupriya
    Sindhupriya avatar
    2 posts
    Member since:
    Feb 2016

    Posted 04 Apr Link to this post

    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. Yoan
    Admin
    Yoan avatar
    1070 posts

    Posted 07 Apr Link to this post

    Hi Sindhu,

    I believe that this stackoverflow 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.
  3. UI for WPF is Visual Studio 2017 Ready
  4. Sindhupriya
    Sindhupriya avatar
    2 posts
    Member since:
    Feb 2016

    Posted 07 Apr in reply to Yoan Link to this post

    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

Back to Top