保存父实体时,linq到实体错误 [英] linq to entities error when saving the parent entity

查看:61
本文介绍了保存父实体时,linq到实体错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我有问题请告知:

我将C#与linq结合使用以连接到sql数据库中的实体,我必须拥有3个表(或实体)AccountingHeader,AccountingDetail和AccountingInterOU
而AccountingDetail和AccountingInterOU具有外键AccountingHeaderID.

当我从AccountingInterOU删除时,一切正常,但是当我保存时,出现此错误:

操作失败:由于一个或多个外键属性不可为空,因此无法更改该关系.对关系进行更改时,相关的外键属性将设置为空值.如果外键不支持空值,则必须定义新的关系,必须为外键属性分配另一个非空值,或者必须删除不相关的对象.

我的代码在这里:

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..

推荐答案

我找到了解决方案....您必须在部分类中添加以下代码:

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)]

,甚至所有您不想更新的孩子都可以使用... regrds

for all child even the one that you don''t want to update...regrds


这篇关于保存父实体时,linq到实体错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆