添加子现有父记录中的实体框架 [英] Add child to existing parent record in entity framework

查看:129
本文介绍了添加子现有父记录中的实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的父母和孩子之间的关系是,他们通过边相连。它是类同的有向图结构。

DAL:

 公共无效SaveResource(资源资源)
    {
        context.AddToResources(资源); //还应该添加的儿童。
        context.SaveChanges();
    }    公共资源占用资源(INT RESOURCEID)
    {
        VAR资源=(从context.Resourcesř
                        .INCLUDE(ToEdges)。包括(FromEdges)
                         其中,r.ResourceId == RESOURCEID
                         选择R).SingleOrDefault();        返回的资源;
    }

服务:

 公共无效AddChildResource(INT parentResourceId,资源childResource)
    {
        资源parentResource = repository.GetResource(parentResourceId);        ResourceEdge inEdge =新ResourceEdge();
        inEdge.ToResource = childResource;        parentResource.ToEdges.Add(inEdge);        repository.SaveResource(parentResource);
    }

错误:具有相同键的对象已经存在于ObjectStateManager。现有的对象是在不变的状态。一个对象只能再次添加到ObjectStateManager如果它是在添加的状态

图片:

有人告诉我,这是提交一个孩子到一个已经存在的父序列:

获取父 - >附加子到父 - >提交父

这是我使用的序列。在code以上使用存储库模式的ASP.NET MVC 2应用程序中抽取。

EDMX文件:

 <?XML版本=1.0编码=UTF-8&GT?;
    < EDMX:EDMX版本=2.0的xmlns:EDMX =htt​​p://schemas.microsoft.com/ado/2008/10/edmx>
      <! - EF运行时的内容 - >
      < EDMX:运行>
        <! - SSDL内容 - >
        < EDMX:StorageModels>
          <模式命名空间=XDbModel.Store别名=自我提供程序=System.Data.SqlClient的ProviderManifestToken =2008的xmlns:店=htt​​p://schemas.microsoft.com/ado/2007/12/ EDM / EntityStoreSchemaGenerator的xmlns =http://schemas.microsoft.com/ado/2009/02/edm/ssdl>
            < EntityContainer相关NAME =XDbModelStoreContainer>
              < EntitySet的名称=捆绑的EntityType =XDbModel.Store.Bundles店:TYPE =表模式=DBO/>
              < EntitySet的名称=CellProviders的EntityType =XDbModel.Store.CellProviders店:TYPE =表模式=DBO/>
              < EntitySet的名称=评论的EntityType =XDbModel.Store.Comments店:TYPE =表模式=DBO/>
              < EntitySet的名称=LocationPoints的EntityType =XDbModel.Store.LocationPoints店:TYPE =表模式=DBO/>
              < EntitySet的名称=位置的EntityType =XDbModel.Store.Locations店:TYPE =表模式=DBO/>
              < EntitySet的名称=商情的EntityType =XDbModel.Store.Offers店:TYPE =表模式=DBO/>
              < EntitySet的名称=PostBundleJunction的EntityType =XDbModel.Store.PostBundleJunction店:TYPE =表模式=DBO/>
              < EntitySet的名称=PostMedia的EntityType =XDbModel.Store.PostMedia店:TYPE =表模式=DBO/>
              < EntitySet的名称=帖子的EntityType =XDbModel.Store.Posts店:TYPE =表模式=DBO/>
              < EntitySet的名称=ResourceEdges的EntityType =XDbModel.Store.ResourceEdges店:TYPE =表模式=DBO/>
              < EntitySet的名称=ResourceNames的EntityType =XDbModel.Store.ResourceNames店:TYPE =表模式=DBO/>
              < EntitySet的名称=资源的EntityType =XDbModel.Store.Resources店:TYPE =表模式=DBO/>
              < EntitySet的名称=sysdiagrams的EntityType =XDbModel.Store.sysdiagrams店:TYPE =表模式=DBO/>
              < EntitySet的名称=用户的EntityType =XDbModel.Store.Users店:TYPE =表模式=DBO/>
              < EntitySet的名称=投票的EntityType =XDbModel.Store.Votes店:TYPE =表模式=DBO/>
              < AssociationSet NAME =FK_Comments_Offers公会=XDbModel.Store.FK_Comments_Offers>
                <作用到底=信息的EntitySet =商情/>
                <作用到底=评论的EntitySet =注释/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Comments_Users公会=XDbModel.Store.FK_Comments_Users>
                <作用到底=用户的EntitySet =用户/>
                <作用到底=评论的EntitySet =注释/>
              < / AssociationSet>
              < AssociationSet NAME =FK_LocationPoints_Locations公会=XDbModel.Store.FK_LocationPoints_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=LocationPoints的EntitySet =LocationPoints/>
              < / AssociationSet>
              < AssociationSet NAME =FK_PostBundleJunction_Bundles公会=XDbModel.Store.FK_PostBundleJunction_Bundles>
                <作用到底=捆绑的EntitySet =捆绑/>
                <作用到底=PostBundleJunction的EntitySet =PostBundleJunction/>
              < / AssociationSet>
              < AssociationSet NAME =FK_PostBundleJunction_Posts公会=XDbModel.Store.FK_PostBundleJunction_Posts>
                <作用到底=帖子的EntitySet =帖子/>
                <作用到底=PostBundleJunction的EntitySet =PostBundleJunction/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_Locations公会=XDbModel.Store.FK_Posts_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_ResourceNames公会=XDbModel.Store.FK_Posts_ResourceNames>
                <作用到底=ResourceNames的EntitySet =ResourceNames/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_Users公会=XDbModel.Store.FK_Posts_Users>
                <作用到底=用户的EntitySet =用户/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceEdges_Resources公会=XDbModel.Store.FK_ResourceEdges_Resources>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceEdges的EntitySet =ResourceEdges/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceEdges_Resources1公会=XDbModel.Store.FK_ResourceEdges_Resources1>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceEdges的EntitySet =ResourceEdges/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceNames_Resources公会=XDbModel.Store.FK_ResourceNames_Resources>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceNames的EntitySet =ResourceNames/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Users_Locations公会=XDbModel.Store.FK_Users_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=用户的EntitySet =用户/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Votes_Posts公会=XDbModel.Store.FK_Votes_Posts>
                <作用到底=帖子的EntitySet =帖子/>
                <作用到底=投票的EntitySet =投票/>
              < / AssociationSet>
            < / EntityContainer相关>
            <的EntityType名称=捆绑>
              <关键>
                < PropertyRef NAME =BundleId/>
              < / Key与GT;
              <属性名=BundleIdTYPE =INT可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=CellProviders>
              <关键>
                < PropertyRef NAME =CellProviderID/>
              < / Key与GT;
              <属性名=CellProviderIDTYPE =唯一标识符可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=意见>
              <关键>
                < PropertyRef NAME =CommentID/>
              < / Key与GT;
              <属性名=CommentIDTYPE =唯一标识符可空=假/>
              <属性名=ParentPostIDTYPE =唯一标识符/>
              <属性名=OfferPostIDTYPE =唯一标识符/>
              <属性名=用户名TYPE =唯一标识符可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=LocationPoints>
              <关键>
                < PropertyRef NAME =LocationPointId/>
              < / Key与GT;
              <属性名=LocationPointIdTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=纬度TYPE =浮动可空=假/>
              <属性名=经度TYPE =浮动可空=假/>
              <属性名=高度TYPE =浮动可空=假/>
              <属性名=计数TYPE =INT可空=假/>
              <属性名=LocationIdTYPE =INT可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=位置>
              <关键>
                < PropertyRef NAME =LocationId/>
              < / Key与GT;
              <属性名=LocationIdTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=地址1TYPE =nvarchar的可空=假MAXLENGTH =200/>
              <属性名=地址2TYPE =nvarchar的MAXLENGTH =200/>
              <属性名=地址3TYPE =nvarchar的MAXLENGTH =200/>
              <属性名=国家TYPE =nvarchar的可空=假MAXLENGTH =200/>
              <属性名=国家TYPE =nvarchar的可空=假MAXLENGTH =200/>
            < /&的EntityType GT;
            <的EntityType名称=优惠>
              <关键>
                < PropertyRef NAME =OFFERID/>
              < / Key与GT;
              <属性名=OFFERIDTYPE =唯一标识符可空=假/>
              <属性名=NeedOfferTYPE =唯一标识符可空=假/>
              <属性名=ProvisionOfferTYPE =唯一标识符可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=PostBundleJunction>
              <关键>
                < PropertyRef NAME =BundleId/>
                < PropertyRef NAME =帖子ID/>
              < / Key与GT;
              <属性名=BundleIdTYPE =INT可空=假/>
              <属性名=帖子IDTYPE =INT可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=PostMedia>
              <关键>
                < PropertyRef NAME =MediaId/>
              < / Key与GT;
              <属性名=MediaIdTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=MediaExtTYPE =nvarchar的可空=假MAXLENGTH =10/>
              <属性名=帖子IDTYPE =INT可空=假/>
              <属性名=SynthIdTYPE =唯一标识符/>
            < /&的EntityType GT;
            <的EntityType名称=帖子>
              <关键>
                < PropertyRef NAME =帖子ID/>
              < / Key与GT;
              <属性名=帖子IDTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=PosterIDTYPE =唯一标识符可空=假/>
              <属性名=BundleIdTYPE =INT可空=假/>
              <属性名=LocationIdTYPE =INT可空=假/>
              <属性名=标签TYPE =唯一标识符/>
              <属性名=QuanitityTYPE =INT可空=假/>
              <属性名称=说明类型=文本可空=假/>
              <属性名=ResourceNameIdTYPE =INT可空=假/>
              <属性名=日期类型=日期时间可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=ResourceEdges>
              <关键>
                < PropertyRef NAME =EdgeID的/>
              < / Key与GT;
              <属性名=等级TYPE =INT可空=假/>
              <属性名=EdgeID的TYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=ToResourceIdTYPE =INT可空=假/>
              <属性名=FromResourrceIdTYPE =INT可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=ResourceNames>
              <关键>
                < PropertyRef NAME =ResourceNameId/>
              < / Key与GT;
              <属性名=ResourceNameIdTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名称=名称TYPE =nvarchar的可空=假MAXLENGTH =100/>
              <属性名=RESOURCEIDTYPE =INT可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=资源>
              <关键>
                < PropertyRef NAME =RESOURCEID/>
              < / Key与GT;
              <属性名=RESOURCEIDTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名称=说明TYPE =nvarchar的MAXLENGTH =50/>
            < /&的EntityType GT;
            <的EntityType名称=sysdiagrams>
              <关键>
                < PropertyRef NAME =diagram_id/>
              < / Key与GT;
              <属性名称=名称TYPE =nvarchar的可空=假MAXLENGTH =128/>
              <属性名=principal_idTYPE =INT可空=假/>
              <属性名=diagram_idTYPE =INT可空=假StoreGeneratedPattern =身份/>
              <属性名=版本类型=整数/>
              <属性名=定义TYPE =VARBINARY(最大)/>
            < /&的EntityType GT;
            <的EntityType名称=用户>
              <关键>
                < PropertyRef名称=用户名/>
              < / Key与GT;
              <属性名=用户名TYPE =唯一标识符可空=假/>
              <属性名=LocationId类型=整数/>
            < /&的EntityType GT;
            <的EntityType名称=投票>
              <关键>
                < PropertyRef NAME =VoteId/>
              < / Key与GT;
              <属性名=VoteIdTYPE =INT可空=假/>
              <属性名=VoterIdTYPE =唯一标识符可空=假/>
              <属性名=VoteContentTYPE =INT可空=假/>
              <属性名=帖子IDTYPE =INT可空=假/>
            < /&的EntityType GT;
            <协会名称=FK_Comments_Offers>
              <作用到底=商情TYPE =XDbModel.Store.Offers多重=0..1/>
              <作用到底=注释TYPE =XDbModel.Store.Comments多重=*/>
              < ReferentialConstraint>
                <校长角色=优惠>
                  < PropertyRef NAME =OFFERID/>
                < /首席>
                <从属角色=意见>
                  < PropertyRef NAME =OfferPostID/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Comments_Users>
              <作用到底=用户TYPE =XDbModel.Store.Users多重=1>
                < OnDelete行动=级联/>
              < /结束>
              <作用到底=注释TYPE =XDbModel.Store.Comments多重=*/>
              < ReferentialConstraint>
                <校长角色=用户>
                  < PropertyRef名称=用户名/>
                < /首席>
                <从属角色=意见>
                  < PropertyRef名称=用户名/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_LocationPoints_Locations>
              <作用到底=位置TYPE =XDbModel.Store.Locations多重=1>
                < OnDelete行动=级联/>
              < /结束>
              <作用到底=LocationPointsTYPE =XDbModel.Store.LocationPoints多重=*/>
              < ReferentialConstraint>
                <校长角色=位置>
                  < PropertyRef NAME =LocationId/>
                < /首席>
                <从属角色=LocationPoints>
                  < PropertyRef NAME =LocationId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_PostBundleJunction_Bundles>
              <作用到底=捆绑TYPE =XDbModel.Store.Bundles多重=1/>
              <作用到底=PostBundleJunctionTYPE =XDbModel.Store.PostBundleJunction多重=*/>
              < ReferentialConstraint>
                <校长角色=捆绑>
                  < PropertyRef NAME =BundleId/>
                < /首席>
                <从属角色=PostBundleJunction>
                  < PropertyRef NAME =BundleId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_PostBundleJunction_Posts>
              <作用到底=帖子TYPE =XDbModel.Store.Posts多重=1/>
              <作用到底=PostBundleJunctionTYPE =XDbModel.Store.PostBundleJunction多重=*/>
              < ReferentialConstraint>
                <校长角色=帖子>
                  < PropertyRef NAME =帖子ID/>
                < /首席>
                <从属角色=PostBundleJunction>
                  < PropertyRef NAME =帖子ID/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Posts_Locations>
              <作用到底=位置TYPE =XDbModel.Store.Locations多重=1/>
              <作用到底=帖子TYPE =XDbModel.Store.Posts多重=*/>
              < ReferentialConstraint>
                <校长角色=位置>
                  < PropertyRef NAME =LocationId/>
                < /首席>
                <从属角色=帖子>
                  < PropertyRef NAME =LocationId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Posts_ResourceNames>
              <作用到底=ResourceNamesTYPE =XDbModel.Store.ResourceNames多重=1/>
              <作用到底=帖子TYPE =XDbModel.Store.Posts多重=*/>
              < ReferentialConstraint>
                <校长角色=ResourceNames>
                  < PropertyRef NAME =ResourceNameId/>
                < /首席>
                <从属角色=帖子>
                  < PropertyRef NAME =ResourceNameId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Posts_Users>
              <作用到底=用户TYPE =XDbModel.Store.Users多重=1/>
              <作用到底=帖子TYPE =XDbModel.Store.Posts多重=*/>
              < ReferentialConstraint>
                <校长角色=用户>
                  < PropertyRef名称=用户名/>
                < /首席>
                <从属角色=帖子>
                  < PropertyRef NAME =PosterID/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_ResourceEdges_Resources>
              <作用到底=资源类型=XDbModel.Store.Resources多重=1/>
              <作用到底=ResourceEdgesTYPE =XDbModel.Store.ResourceEdges多重=*/>
              < ReferentialConstraint>
                <校长角色=资源>
                  < PropertyRef NAME =RESOURCEID/>
                < /首席>
                <从属角色=ResourceEdges>
                  < PropertyRef NAME =ToResourceId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_ResourceEdges_Resources1>
              <作用到底=资源类型=XDbModel.Store.Resources多重=1/>
              <作用到底=ResourceEdgesTYPE =XDbModel.Store.ResourceEdges多重=*/>
              < ReferentialConstraint>
                <校长角色=资源>
                  < PropertyRef NAME =RESOURCEID/>
                < /首席>
                <从属角色=ResourceEdges>
                  < PropertyRef NAME =FromResourrceId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_ResourceNames_Resources>
              <作用到底=资源类型=XDbModel.Store.Resources多重=1>
                < OnDelete行动=级联/>
              < /结束>
              <作用到底=ResourceNamesTYPE =XDbModel.Store.ResourceNames多重=*/>
              < ReferentialConstraint>
                <校长角色=资源>
                  < PropertyRef NAME =RESOURCEID/>
                < /首席>
                <从属角色=ResourceNames>
                  < PropertyRef NAME =RESOURCEID/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Users_Locations>
              <作用到底=位置TYPE =XDbModel.Store.Locations多重=0..1>
                < OnDelete行动=级联/>
              < /结束>
              <作用到底=用户TYPE =XDbModel.Store.Users多重=*/>
              < ReferentialConstraint>
                <校长角色=位置>
                  < PropertyRef NAME =LocationId/>
                < /首席>
                <从属角色=用户>
                  < PropertyRef NAME =LocationId/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
            <协会名称=FK_Votes_Posts>
              <作用到底=帖子TYPE =XDbModel.Store.Posts多重=1>
                < OnDelete行动=级联/>
              < /结束>
              <作用到底=投票TYPE =XDbModel.Store.Votes多重=*/>
              < ReferentialConstraint>
                <校长角色=帖子>
                  < PropertyRef NAME =帖子ID/>
                < /首席>
                <从属角色=投票>
                  < PropertyRef NAME =帖子ID/>
                < /依赖型GT;
              < / ReferentialConstraint>
            < /协会>
          < /模式>
        < / EDMX:StorageModels>
        <! - CSDL内容 - >
        < EDMX:ConceptualModels>
          <模式命名空间=XDbModel别名=自我的xmlns:注释=htt​​p://schemas.microsoft.com/ado/2009/02/edm/annotation的xmlns =htt​​p://schemas.microsoft.com / ADO / 2008/09 / EDM>
            < EntityContainer相关NAME =XDbEntities注释:LazyLoadingEnabled =真正的>
              < EntitySet的名称=捆绑的EntityType =XDbModel.Bundle/>
              < EntitySet的名称=CellProviders的EntityType =XDbModel.CellProvider/>
              < EntitySet的名称=评论的EntityType =XDbModel.Comment/>
              < EntitySet的名称=LocationPoints的EntityType =XDbModel.LocationPoint/>
              < EntitySet的名称=位置的EntityType =XDbModel.Location/>
              < EntitySet的名称=商情的EntityType =XDbModel.Offer/>
              < EntitySet的名称=PostMedias的EntityType =XDbModel.PostMedia/>
              < EntitySet的名称=帖子的EntityType =XDbModel.Post/>
              < EntitySet的名称=ResourceEdges的EntityType =XDbModel.ResourceEdge/>
              < EntitySet的名称=ResourceNames的EntityType =XDbModel.ResourceName/>
              < EntitySet的名称=资源的EntityType =XDbModel.Resource/>
              < EntitySet的名称=sysdiagrams的EntityType =XDbModel.sysdiagram/>
              < EntitySet的名称=用户的EntityType =XDbModel.User/>
              < EntitySet的名称=投票的EntityType =XDbModel.Vote/>
              < AssociationSet NAME =FK_Comments_Offers公会=XDbModel.FK_Comments_Offers>
                <作用到底=信息的EntitySet =商情/>
                <作用到底=评论的EntitySet =注释/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Comments_Users公会=XDbModel.FK_Comments_Users>
                <作用到底=用户的EntitySet =用户/>
                <作用到底=评论的EntitySet =注释/>
              < / AssociationSet>
              < AssociationSet NAME =FK_LocationPoints_Locations公会=XDbModel.FK_LocationPoints_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=LocationPoints的EntitySet =LocationPoints/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_Locations公会=XDbModel.FK_Posts_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Users_Locations公会=XDbModel.FK_Users_Locations>
                <作用到底=位置的EntitySet =位置/>
                <作用到底=用户的EntitySet =用户/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_ResourceNames公会=XDbModel.FK_Posts_ResourceNames>
                <作用到底=ResourceNames的EntitySet =ResourceNames/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Posts_Users公会=XDbModel.FK_Posts_Users>
                <作用到底=用户的EntitySet =用户/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
              < AssociationSet NAME =FK_Votes_Posts公会=XDbModel.FK_Votes_Posts>
                <作用到底=帖子的EntitySet =帖子/>
                <作用到底=投票的EntitySet =投票/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceEdges_Resources公会=XDbModel.FK_ResourceEdges_Resources>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceEdges的EntitySet =ResourceEdges/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceEdges_Resources1公会=XDbModel.FK_ResourceEdges_Resources1>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceEdges的EntitySet =ResourceEdges/>
              < / AssociationSet>
              < AssociationSet NAME =FK_ResourceNames_Resources公会=XDbModel.FK_ResourceNames_Resources>
                <作用到底=资源的EntitySet =资源/>
                <作用到底=ResourceNames的EntitySet =ResourceNames/>
              < / AssociationSet>
              < AssociationSet NAME =PostBundleJunction公会=XDbModel.PostBundleJunction>
                <作用到底=捆绑的EntitySet =捆绑/>
                <作用到底=帖子的EntitySet =帖子/>
              < / AssociationSet>
            < / EntityContainer相关>
            <的EntityType名称=捆绑>
              <关键>
                < PropertyRef NAME =BundleId/>
              < / Key与GT;
              <属性名=BundleIdTYPE =的Int32可空=假/>
              < NavigationProperty NAME =帖子的关系=XDbModel.PostBundleJunctionFromRole =捆绑ToRole =帖子/>
            < /&的EntityType GT;
            <的EntityType名称=CellProvider>
              <关键>
                < PropertyRef NAME =CellProviderID/>
              < / Key与GT;
              <属性名=CellProviderID类型=GUID可空=假/>
            < /&的EntityType GT;
            <的EntityType名称=注释>
              <关键>
                < PropertyRef NAME =CommentID/>


解决方案

啊,这实际上非常简单,一旦你想想..
什么你要做到这一点资源添加到数据上下文,即使该资源对象是从摆在首位数据库的信息。因此,错误不是抱怨你的孩子的对象,但你的父对象! :)

基本上,如果你对此有何评论的第一行应该只是工作罚款SaveResource()方法!
例如:

 公共无效SaveResource(资源资源)
{
    // context.AddToResources(资源); //还应该添加的儿童。
    context.SaveChanges();
}

或者只是移动AddChildResource方法您的DAL:

 公共无效AddChildResource(INT parentResourceId,资源childResource)
{
    资源parentResource = repository.GetResource(parentResourceId);    ResourceEdge inEdge =新ResourceEdge();
    inEdge.ToResource = childResource;    parentResource.ToEdges.Add(inEdge);    context.SaveChanges();
}

My relationship between the parent and child is that they are connected by an edge. It is similiar to a directed graph structure.

DAL:

    public void SaveResource(Resource resource)
    {
        context.AddToResources(resource); //Should also add children.
        context.SaveChanges();
    }

    public Resource GetResource(int resourceId)
    {
        var resource = (from r in context.Resources
                        .Include("ToEdges").Include("FromEdges")
                         where r.ResourceId == resourceId
                         select r).SingleOrDefault();

        return resource;
    }

Service:

    public void  AddChildResource(int parentResourceId, Resource childResource)
    {
        Resource parentResource = repository.GetResource(parentResourceId);

        ResourceEdge inEdge = new ResourceEdge();
        inEdge.ToResource = childResource;

        parentResource.ToEdges.Add(inEdge);

        repository.SaveResource(parentResource);
    }

Error: An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.

Image:

I have been told this is the sequence in submitting a child to an already existing parent:

Get parent -> Attach Child to parent -> submit parent.

That is the sequence I used. The code above is extracted from an ASP.NET MVC 2 application using the repository pattern.

EDMX file:

    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
      <!-- EF Runtime content -->
      <edmx:Runtime>
        <!-- SSDL content -->
        <edmx:StorageModels>
          <Schema Namespace="XDbModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
            <EntityContainer Name="XDbModelStoreContainer">
              <EntitySet Name="Bundles" EntityType="XDbModel.Store.Bundles" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="CellProviders" EntityType="XDbModel.Store.CellProviders" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Comments" EntityType="XDbModel.Store.Comments" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="LocationPoints" EntityType="XDbModel.Store.LocationPoints" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Locations" EntityType="XDbModel.Store.Locations" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Offers" EntityType="XDbModel.Store.Offers" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="PostBundleJunction" EntityType="XDbModel.Store.PostBundleJunction" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="PostMedia" EntityType="XDbModel.Store.PostMedia" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Posts" EntityType="XDbModel.Store.Posts" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="ResourceEdges" EntityType="XDbModel.Store.ResourceEdges" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="ResourceNames" EntityType="XDbModel.Store.ResourceNames" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Resources" EntityType="XDbModel.Store.Resources" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="sysdiagrams" EntityType="XDbModel.Store.sysdiagrams" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Users" EntityType="XDbModel.Store.Users" store:Type="Tables" Schema="dbo" />
              <EntitySet Name="Votes" EntityType="XDbModel.Store.Votes" store:Type="Tables" Schema="dbo" />
              <AssociationSet Name="FK_Comments_Offers" Association="XDbModel.Store.FK_Comments_Offers">
                <End Role="Offers" EntitySet="Offers" />
                <End Role="Comments" EntitySet="Comments" />
              </AssociationSet>
              <AssociationSet Name="FK_Comments_Users" Association="XDbModel.Store.FK_Comments_Users">
                <End Role="Users" EntitySet="Users" />
                <End Role="Comments" EntitySet="Comments" />
              </AssociationSet>
              <AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.Store.FK_LocationPoints_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="LocationPoints" EntitySet="LocationPoints" />
              </AssociationSet>
              <AssociationSet Name="FK_PostBundleJunction_Bundles" Association="XDbModel.Store.FK_PostBundleJunction_Bundles">
                <End Role="Bundles" EntitySet="Bundles" />
                <End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
              </AssociationSet>
              <AssociationSet Name="FK_PostBundleJunction_Posts" Association="XDbModel.Store.FK_PostBundleJunction_Posts">
                <End Role="Posts" EntitySet="Posts" />
                <End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_Locations" Association="XDbModel.Store.FK_Posts_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.Store.FK_Posts_ResourceNames">
                <End Role="ResourceNames" EntitySet="ResourceNames" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_Users" Association="XDbModel.Store.FK_Posts_Users">
                <End Role="Users" EntitySet="Users" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.Store.FK_ResourceEdges_Resources">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceEdges" EntitySet="ResourceEdges" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.Store.FK_ResourceEdges_Resources1">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceEdges" EntitySet="ResourceEdges" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.Store.FK_ResourceNames_Resources">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceNames" EntitySet="ResourceNames" />
              </AssociationSet>
              <AssociationSet Name="FK_Users_Locations" Association="XDbModel.Store.FK_Users_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="Users" EntitySet="Users" />
              </AssociationSet>
              <AssociationSet Name="FK_Votes_Posts" Association="XDbModel.Store.FK_Votes_Posts">
                <End Role="Posts" EntitySet="Posts" />
                <End Role="Votes" EntitySet="Votes" />
              </AssociationSet>
            </EntityContainer>
            <EntityType Name="Bundles">
              <Key>
                <PropertyRef Name="BundleId" />
              </Key>
              <Property Name="BundleId" Type="int" Nullable="false" />
            </EntityType>
            <EntityType Name="CellProviders">
              <Key>
                <PropertyRef Name="CellProviderID" />
              </Key>
              <Property Name="CellProviderID" Type="uniqueidentifier" Nullable="false" />
            </EntityType>
            <EntityType Name="Comments">
              <Key>
                <PropertyRef Name="CommentID" />
              </Key>
              <Property Name="CommentID" Type="uniqueidentifier" Nullable="false" />
              <Property Name="ParentPostID" Type="uniqueidentifier" />
              <Property Name="OfferPostID" Type="uniqueidentifier" />
              <Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
            </EntityType>
            <EntityType Name="LocationPoints">
              <Key>
                <PropertyRef Name="LocationPointId" />
              </Key>
              <Property Name="LocationPointId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="Latitude" Type="float" Nullable="false" />
              <Property Name="Longitude" Type="float" Nullable="false" />
              <Property Name="Altitude" Type="float" Nullable="false" />
              <Property Name="Count" Type="int" Nullable="false" />
              <Property Name="LocationId" Type="int" Nullable="false" />
            </EntityType>
            <EntityType Name="Locations">
              <Key>
                <PropertyRef Name="LocationId" />
              </Key>
              <Property Name="LocationId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="Address1" Type="nvarchar" Nullable="false" MaxLength="200" />
              <Property Name="Address2" Type="nvarchar" MaxLength="200" />
              <Property Name="Address3" Type="nvarchar" MaxLength="200" />
              <Property Name="State" Type="nvarchar" Nullable="false" MaxLength="200" />
              <Property Name="Country" Type="nvarchar" Nullable="false" MaxLength="200" />
            </EntityType>
            <EntityType Name="Offers">
              <Key>
                <PropertyRef Name="OfferID" />
              </Key>
              <Property Name="OfferID" Type="uniqueidentifier" Nullable="false" />
              <Property Name="NeedOffer" Type="uniqueidentifier" Nullable="false" />
              <Property Name="ProvisionOffer" Type="uniqueidentifier" Nullable="false" />
            </EntityType>
            <EntityType Name="PostBundleJunction">
              <Key>
                <PropertyRef Name="BundleId" />
                <PropertyRef Name="PostId" />
              </Key>
              <Property Name="BundleId" Type="int" Nullable="false" />
              <Property Name="PostId" Type="int" Nullable="false" />
            </EntityType>
            <EntityType Name="PostMedia">
              <Key>
                <PropertyRef Name="MediaId" />
              </Key>
              <Property Name="MediaId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="MediaExt" Type="nvarchar" Nullable="false" MaxLength="5" />
              <Property Name="PostId" Type="int" Nullable="false" />
              <Property Name="SynthId" Type="uniqueidentifier" />
            </EntityType>
            <EntityType Name="Posts">
              <Key>
                <PropertyRef Name="PostId" />
              </Key>
              <Property Name="PostId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="PosterID" Type="uniqueidentifier" Nullable="false" />
              <Property Name="BundleId" Type="int" Nullable="false" />
              <Property Name="LocationId" Type="int" Nullable="false" />
              <Property Name="Tags" Type="uniqueidentifier" />
              <Property Name="Quanitity" Type="int" Nullable="false" />
              <Property Name="Description" Type="text" Nullable="false" />
              <Property Name="ResourceNameId" Type="int" Nullable="false" />
              <Property Name="Date" Type="datetime" Nullable="false" />
            </EntityType>
            <EntityType Name="ResourceEdges">
              <Key>
                <PropertyRef Name="EdgeId" />
              </Key>
              <Property Name="Rank" Type="int" Nullable="false" />
              <Property Name="EdgeId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="ToResourceId" Type="int" Nullable="false" />
              <Property Name="FromResourrceId" Type="int" Nullable="false" />
            </EntityType>
            <EntityType Name="ResourceNames">
              <Key>
                <PropertyRef Name="ResourceNameId" />
              </Key>
              <Property Name="ResourceNameId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="100" />
              <Property Name="ResourceId" Type="int" Nullable="false" />
            </EntityType>
            <EntityType Name="Resources">
              <Key>
                <PropertyRef Name="ResourceId" />
              </Key>
              <Property Name="ResourceId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="Description" Type="nvarchar" MaxLength="50" />
            </EntityType>
            <EntityType Name="sysdiagrams">
              <Key>
                <PropertyRef Name="diagram_id" />
              </Key>
              <Property Name="name" Type="nvarchar" Nullable="false" MaxLength="128" />
              <Property Name="principal_id" Type="int" Nullable="false" />
              <Property Name="diagram_id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="version" Type="int" />
              <Property Name="definition" Type="varbinary(max)" />
            </EntityType>
            <EntityType Name="Users">
              <Key>
                <PropertyRef Name="UserID" />
              </Key>
              <Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
              <Property Name="LocationId" Type="int" />
            </EntityType>
            <EntityType Name="Votes">
              <Key>
                <PropertyRef Name="VoteId" />
              </Key>
              <Property Name="VoteId" Type="int" Nullable="false" />
              <Property Name="VoterId" Type="uniqueidentifier" Nullable="false" />
              <Property Name="VoteContent" Type="int" Nullable="false" />
              <Property Name="PostId" Type="int" Nullable="false" />
            </EntityType>
            <Association Name="FK_Comments_Offers">
              <End Role="Offers" Type="XDbModel.Store.Offers" Multiplicity="0..1" />
              <End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Offers">
                  <PropertyRef Name="OfferID" />
                </Principal>
                <Dependent Role="Comments">
                  <PropertyRef Name="OfferPostID" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Comments_Users">
              <End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1">
                <OnDelete Action="Cascade" />
              </End>
              <End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Users">
                  <PropertyRef Name="UserID" />
                </Principal>
                <Dependent Role="Comments">
                  <PropertyRef Name="UserID" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_LocationPoints_Locations">
              <End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1">
                <OnDelete Action="Cascade" />
              </End>
              <End Role="LocationPoints" Type="XDbModel.Store.LocationPoints" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Locations">
                  <PropertyRef Name="LocationId" />
                </Principal>
                <Dependent Role="LocationPoints">
                  <PropertyRef Name="LocationId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_PostBundleJunction_Bundles">
              <End Role="Bundles" Type="XDbModel.Store.Bundles" Multiplicity="1" />
              <End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Bundles">
                  <PropertyRef Name="BundleId" />
                </Principal>
                <Dependent Role="PostBundleJunction">
                  <PropertyRef Name="BundleId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_PostBundleJunction_Posts">
              <End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1" />
              <End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Posts">
                  <PropertyRef Name="PostId" />
                </Principal>
                <Dependent Role="PostBundleJunction">
                  <PropertyRef Name="PostId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Posts_Locations">
              <End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1" />
              <End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Locations">
                  <PropertyRef Name="LocationId" />
                </Principal>
                <Dependent Role="Posts">
                  <PropertyRef Name="LocationId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Posts_ResourceNames">
              <End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="1" />
              <End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="ResourceNames">
                  <PropertyRef Name="ResourceNameId" />
                </Principal>
                <Dependent Role="Posts">
                  <PropertyRef Name="ResourceNameId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Posts_Users">
              <End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1" />
              <End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Users">
                  <PropertyRef Name="UserID" />
                </Principal>
                <Dependent Role="Posts">
                  <PropertyRef Name="PosterID" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_ResourceEdges_Resources">
              <End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
              <End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Resources">
                  <PropertyRef Name="ResourceId" />
                </Principal>
                <Dependent Role="ResourceEdges">
                  <PropertyRef Name="ToResourceId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_ResourceEdges_Resources1">
              <End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
              <End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Resources">
                  <PropertyRef Name="ResourceId" />
                </Principal>
                <Dependent Role="ResourceEdges">
                  <PropertyRef Name="FromResourrceId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_ResourceNames_Resources">
              <End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1">
                <OnDelete Action="Cascade" />
              </End>
              <End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Resources">
                  <PropertyRef Name="ResourceId" />
                </Principal>
                <Dependent Role="ResourceNames">
                  <PropertyRef Name="ResourceId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Users_Locations">
              <End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="0..1">
                <OnDelete Action="Cascade" />
              </End>
              <End Role="Users" Type="XDbModel.Store.Users" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Locations">
                  <PropertyRef Name="LocationId" />
                </Principal>
                <Dependent Role="Users">
                  <PropertyRef Name="LocationId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
            <Association Name="FK_Votes_Posts">
              <End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1">
                <OnDelete Action="Cascade" />
              </End>
              <End Role="Votes" Type="XDbModel.Store.Votes" Multiplicity="*" />
              <ReferentialConstraint>
                <Principal Role="Posts">
                  <PropertyRef Name="PostId" />
                </Principal>
                <Dependent Role="Votes">
                  <PropertyRef Name="PostId" />
                </Dependent>
              </ReferentialConstraint>
            </Association>
          </Schema>
        </edmx:StorageModels>
        <!-- CSDL content -->
        <edmx:ConceptualModels>
          <Schema Namespace="XDbModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
            <EntityContainer Name="XDbEntities" annotation:LazyLoadingEnabled="true">
              <EntitySet Name="Bundles" EntityType="XDbModel.Bundle" />
              <EntitySet Name="CellProviders" EntityType="XDbModel.CellProvider" />
              <EntitySet Name="Comments" EntityType="XDbModel.Comment" />
              <EntitySet Name="LocationPoints" EntityType="XDbModel.LocationPoint" />
              <EntitySet Name="Locations" EntityType="XDbModel.Location" />
              <EntitySet Name="Offers" EntityType="XDbModel.Offer" />
              <EntitySet Name="PostMedias" EntityType="XDbModel.PostMedia" />
              <EntitySet Name="Posts" EntityType="XDbModel.Post" />
              <EntitySet Name="ResourceEdges" EntityType="XDbModel.ResourceEdge" />
              <EntitySet Name="ResourceNames" EntityType="XDbModel.ResourceName" />
              <EntitySet Name="Resources" EntityType="XDbModel.Resource" />
              <EntitySet Name="sysdiagrams" EntityType="XDbModel.sysdiagram" />
              <EntitySet Name="Users" EntityType="XDbModel.User" />
              <EntitySet Name="Votes" EntityType="XDbModel.Vote" />
              <AssociationSet Name="FK_Comments_Offers" Association="XDbModel.FK_Comments_Offers">
                <End Role="Offers" EntitySet="Offers" />
                <End Role="Comments" EntitySet="Comments" />
              </AssociationSet>
              <AssociationSet Name="FK_Comments_Users" Association="XDbModel.FK_Comments_Users">
                <End Role="Users" EntitySet="Users" />
                <End Role="Comments" EntitySet="Comments" />
              </AssociationSet>
              <AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.FK_LocationPoints_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="LocationPoints" EntitySet="LocationPoints" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_Locations" Association="XDbModel.FK_Posts_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_Users_Locations" Association="XDbModel.FK_Users_Locations">
                <End Role="Locations" EntitySet="Locations" />
                <End Role="Users" EntitySet="Users" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.FK_Posts_ResourceNames">
                <End Role="ResourceNames" EntitySet="ResourceNames" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_Posts_Users" Association="XDbModel.FK_Posts_Users">
                <End Role="Users" EntitySet="Users" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
              <AssociationSet Name="FK_Votes_Posts" Association="XDbModel.FK_Votes_Posts">
                <End Role="Posts" EntitySet="Posts" />
                <End Role="Votes" EntitySet="Votes" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.FK_ResourceEdges_Resources">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceEdges" EntitySet="ResourceEdges" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.FK_ResourceEdges_Resources1">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceEdges" EntitySet="ResourceEdges" />
              </AssociationSet>
              <AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.FK_ResourceNames_Resources">
                <End Role="Resources" EntitySet="Resources" />
                <End Role="ResourceNames" EntitySet="ResourceNames" />
              </AssociationSet>
              <AssociationSet Name="PostBundleJunction" Association="XDbModel.PostBundleJunction">
                <End Role="Bundles" EntitySet="Bundles" />
                <End Role="Posts" EntitySet="Posts" />
              </AssociationSet>
            </EntityContainer>
            <EntityType Name="Bundle">
              <Key>
                <PropertyRef Name="BundleId" />
              </Key>
              <Property Name="BundleId" Type="Int32" Nullable="false" />
              <NavigationProperty Name="Posts" Relationship="XDbModel.PostBundleJunction" FromRole="Bundles" ToRole="Posts" />
            </EntityType>
            <EntityType Name="CellProvider">
              <Key>
                <PropertyRef Name="CellProviderID" />
              </Key>
              <Property Name="CellProviderID" Type="Guid" Nullable="false" />
            </EntityType>
            <EntityType Name="Comment">
              <Key>
                <PropertyRef Name="CommentID" />

解决方案

Ah, it's actually very simple, once you think about it.. What you're trying to do it add the resource to the data context, even though this resource object was received from the database in the first place. So the error is not complaining about your child objects, but about your parent object! :)

Basically if you comment the first line in the SaveResource() method it should just work fine! E.g.:

public void SaveResource(Resource resource)
{
    // context.AddToResources(resource); //Should also add children.
    context.SaveChanges();
}

Or just move the AddChildResource method to your DAL:

public void AddChildResource(int parentResourceId, Resource childResource)
{
    Resource parentResource = repository.GetResource(parentResourceId);

    ResourceEdge inEdge = new ResourceEdge();
    inEdge.ToResource = childResource;

    parentResource.ToEdges.Add(inEdge);

    context.SaveChanges();
}

这篇关于添加子现有父记录中的实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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