将子项添加到实体框架中的现有父记录

2023-12-31

我的父母和孩子之间的关系是通过边缘连接的。它类似于有向图结构。

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);
    }

错误:ObjectStateManager 中已存在具有相同键的对象。现有对象处于 Unchanged 状态。只有处于已添加状态的对象才能再次添加到 ObjectStateManager 中。

Image:

有人告诉我这是将孩子提交给现有父母的顺序:

获取父级 -> 将子级附加到父级 -> 提交父级。

这就是我使用的顺序。上面的代码是使用存储库模式从 ASP.NET MVC 2 应用程序中提取的。

EDMX 文件:

    <?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" />

啊,其实很简单,只要你想一想.. 您尝试执行的操作是将资源添加到数据上下文中,即使该资源对象首先是从数据库接收的。所以错误不是抱怨你的子对象,而是抱怨你的父对象! :)

基本上,如果您注释 SaveResource() 方法中的第一行,它应该可以正常工作! 例如。:

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

或者只是将 AddChildResource 方法移至您的 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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将子项添加到实体框架中的现有父记录 的相关文章

  • LINQ to Entities 如何更新记录

    好的 我对 EF 和 LINQ 都是新手 我已经弄清楚如何插入和删除 但由于某种原因更新似乎逃脱了我的掌握 这是我的代码示例 EntityDB dataBase new EntityDB Customer c new Customer Na
  • 配置多个数据库Entity Framework 6

    在我的解决方案中 我有 2 个使用 Entity Framework 6 的项目 每个项目都指向不同的数据库 都使用相同的数据提供 SQL Server 我的解决方案中的第三个项目需要使用这两个数据库 我的问题是如何配置这些上下文 我尝试在
  • Entity Framework 6 Code First 迁移 - 用于生产的多个分支

    在我的项目中 我们有一个分支模型 它有一个单独的开发分支 并且每个版本都有一个单独的分支 它可能看起来像这样 dev master r1 r2 因此 我们在 dev 上开发并将其合并到 master 然后创建一个发布分支 r1 r2 我们想
  • 实体框架代码优先查找与 SingleOrDefault(预加载)

    我正在使用 Entity Framework 4 2 代码优先 来访问我的数据库 我假设如果我使用查询一个实体SingleOrDefault如果实体尚未被跟踪 它只会查询数据库 但情况似乎并非如此 这Find另一方面 方法似乎确实在这样做
  • 部分 ASP.NET MVC 视图提交

    我是 ASP NET MVC 的新手 所以这个问题可能显得 愚蠢 抱歉 我的主视图中有一个部分视图 部分视图提交一个表单 调用 HomeController 内的操作方法 它与服务器验证配合得很好 问题是在发布后仅呈现部分视图 发布后如何呈
  • 如何使用 IHttpActionResult 设置标头

    我想将令牌添加到 webapi 请求的标头中 我怎样才能做到这一点 这是我的示例代码 public IHttpActionResult Authenticate Login data var Token fdsf123546fskjhf g
  • 我可以在 Orchard CMS 中使用我的 Ninject .NET 项目吗?

    我正在使用 Orchard CMS 创建一个网站 并且有一个用 Ninject 编写的外部 NET 项目 用于依赖注入 我想将其与 Orchard CMS 中的模块一起使用 我知道 Orchard 使用 Autofac 进行依赖注入 这给我
  • 在 Asp.net MVC4 中访问 ViewBag 时出现 NullReferenceException

    我正在尝试访问ViewBag我认为的数据如下 span class small ViewBag BreadCrumb span 我正在发送那个ViewBag来自代码的数据 例如 ViewBag BreadCrumb topic Catego
  • Oracle Developer Tools for Visual Studio 2019 无法正确安装

    在 VS 2019 中 ODT 使用扩展名安装 而不是像以前的版本那样作为安装文件安装 因此 从 VS 2017 升级的 EF 6 使用的 MVC 项目 edmx 文件在扩展安装后不显示数据库图表 空白黑页 编辑 xml 选项等 仅此而已
  • 如何保护 ASP.NET Web API [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想建立一个RESTful使用 ASP NET Web API 的 Web 服务 第三方开发人员将使用该服务来访问我的应用程序的数据
  • 如何在单击 Html.ActionLink 时加载部分视图?

    有人可以告诉我如何加载吗PartialView单击时在同一页面中Html ActionLink 我在下面列出了我的要求 我有一个索引页面 它将列出 员工 表中的员工 表中的每一行还将有 编辑和删除 链接 Html ActionLink 来更
  • 在 asp.net mvc 3 中使用 Last-Modified 标头和 OutputCacheAttribute 进行客户端缓存

    Edited 我想在客户端缓存图像 并且知道在 mvc 3 中有不同的方法可以做到这一点 如果我错了 请纠正我 1 你可以使用OutputCacheAttribute其工作原理是Expireshttp 标头 但它会回来304 Not Mod
  • 当前上下文中不存在名称“DefaultAuthenticationTypes”

    我正在尝试在我的 Web 应用程序中实现基于角色的授权 如下所示 HttpPost ActionName Login public ActionResult Login LoginViewModel model if ModelState
  • 使用 Entity Framework Core 2.0 更改或重命名列名称而不丢失数据

    我意识到我的一个列标题拼写错误 因此我在模型中更改了它并创建了一个新的迁移以将其更新到数据库中 一切都很完美 直到我意识到实际发生的情况是一个新列取代了现有列并删除了所有数据 碰巧的是 由于这是一个教程数据库 因此恢复数据并不重要 只需几分
  • 默认情况下 dbo 架构中的 EF 6 Code First __MigrationHistory

    我是代码优先实体框架的新手 第一次运行我的应用程序后登录数据库时 当我看到 MigrationHistory 表时 我有点困惑 我现在了解对此表的需求 但不喜欢它位于用户表内的标准 dbo 模式中 我认为它很唐突且有风险 我的第一个想法是将
  • 使用默认行为将模型绑定到接口

    我正在尝试将控制器操作绑定到接口 但仍保持默认的绑定行为 public class CoolClass ISomeInterface public DoSomething get set ISomeInterface public clas
  • 如何使用 Linq 将实体表与交叉引用表连接起来

    首先我要说的是 我对 Linq 比较陌生 但我似乎很快就掌握了其中的大部分内容 但这个问题却难倒了我 我找了又找都没有结果 我使用代码优先并创建了 2 个模型 项目 和 关键字 他们之间存在多对多的关系 我的实体模型如下所示 public
  • 如何在Entity Framework 5中正确触发集合的延迟加载?

    我在我的应用程序中使用 EF5 代码优先 我有一个包含一些延迟加载字段的表 public class TestEntity public int Id get set public virtual TestEntity2 SubEntity
  • SignalR 的浏览器兼容性如何?

    我在网上找到的最多的是 SignalR FAQ 其中指出 SignalR 在 IE6 7 中不起作用 但是 出于法律原因 我需要向客户提供受支持的浏览器列表 SignalR 有这样的经过测试的浏览器列表吗 Thanks 看起来微软已经发布了
  • Visual Studio 2015 EDMX 模型浏览器和图表丢失

    我最近更新到 Visual Studio 2015 打开我的解决方案并运行 直到我决定将一个实体添加到我的 edmx 中 因此 我双击 edmx 文件来打开图表 或者至少是模型浏览器 但它只打开一个 XML 页面 我检查了安装程序中任何丢失

随机推荐