Click here to Skip to main content
15,886,362 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 :

C#
string msg = IsDuplicateAccountingHeader(_currentAccountingHeader.AccountingHeaderID, _currentAccountingHeader.Description,
                    _currentAccountingHeader.AccountingTypeSys.AccountingTypeID, _currentAccountingHeader.OrganizationUnitID,
                    _currentAccountingHeader.InterOrganizationUnitTypeID, null);
                if (!string.IsNullOrEmpty(msg))
                {
                    ShowMessageAccountingDesign(GetLocalResourceObject("ItemDuplicated").ToString());
                 
                    return;
                }
                List<AccountFormatDetail> deletedItems = new List<AccountFormatDetail>();

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

                    // Load the AccountFormat list
                    _srvAccouningDesign.SaveAccounting(_currentAccountingHeader);
                }
                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
                    );
                    db.SaveChanges();


regards..
Posted

1 solution

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

C#
[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
C#
[AssociationEndBehavior("Child1", Owned = true)]
for all child even the one that you don't want to update...regrds
 
Share this answer
 

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