asp.net-mvc – 将子项添加到实体框架中的现有父记录

我和父母之间的关系是他们通过边缘联系起来.它与有向图结构类似.

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

图片:

alt text http://img100.imageshack.us/img100/374/entity.png

我被告知这是将孩子提交给已经存在的父母的顺序:

得父母 – >将孩子附加到父母 – >提交父母.

这就是我使用的序列.上面的代码是使用存储库模式从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();
}
点赞