Click here to Skip to main content
15,064,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi friends,

i have an issue please advise:

i am using C# with linq to entities in to connect to sql database, i have i have to 3 tables (or entities) AccountingHeader,AccountingDetail, and AccountingInterOU
while AccountingDetail and AccountingInterOU have foreign keys AccountingHeaderID.

when i delete from AccountingInterOU every thing is ok but when i save i get this error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

and my code is here :

string msg = IsDuplicateAccountingHeader(_currentAccountingHeader.AccountingHeaderID, _currentAccountingHeader.Description,
                    _currentAccountingHeader.AccountingTypeSys.AccountingTypeID, _currentAccountingHeader.OrganizationUnitID,
                    _currentAccountingHeader.InterOrganizationUnitTypeID, null);
                if (!string.IsNullOrEmpty(msg))
                List<AccountFormatDetail> deletedItems = new List<AccountFormatDetail>();

                    // Create AccountFormat Service
                    IAccountingDesignService _srvAccouningDesign = People365ChannelFactory.CreateChannel<IAccountingDesignService>();

                    // Load the AccountFormat list
                catch (Exception ex) { ManageException(ex, ShowMessageAccountingDesign); return; }

AccountingHeader item = db.AttachObjectGraph(accountingHeader,
                    p => p.AccountingDetail,
                    p => p.AccountingDetail.First().CreditFormat.WithoutUpdate(),
                    p => p.AccountingDetail.First().DebitFormat.WithoutUpdate(),
                    p => p.AccountingTypeSys.AccountingTypeSysLang.First().WithoutUpdate(),
                    p => p.AccountingInterOUs


1 solution

i found the have to add this piece of code in a partial class:

[AssociationEndBehavior("AccountingPositions", Owned = true)]
[AssociationEndBehavior("AccountingInterOUs", Owned = true)]
[AssociationEndBehavior("AccountingDetail", Owned = true)]
public partial class AccountingHeader { }

when deleting the parent object you will get an error if you don't use that code create a partial class and above it write
[AssociationEndBehavior("Child1", Owned = true)]
for all child even the one that you don't want to update...regrds

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900