我父母与孩子之间的关系是它们之间通过一条边相连。它类似于有向图的结构。

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


服务:

    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中已经存在具有相同键的对象。现有对象处于“未更改”状态。如果对象处于添加状态,则只能将其再次添加到ObjectStateManager。

图片:

asp.net-mvc - 将子项添加到 Entity Framework 中的现有父记录-LMLPHP

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

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

那就是我使用的顺序。上面的代码是使用存储库模式从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();
}

关于asp.net-mvc - 将子项添加到 Entity Framework 中的现有父记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2618586/

10-13 06:25