Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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))
                {
                    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 16-Nov-12 1:53am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

i found the solution....you 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
  Permalink  

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

  Print Answers RSS
0 Sascha Lefévre 485
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 255
3 Abhinav S 185
4 Peter Leow 110
0 Sergey Alexandrovich Kryukov 6,953
1 OriginalGriff 6,311
2 Maciej Los 2,732
3 Peter Leow 2,694
4 Abhinav S 2,652


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 20 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100