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

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

问题描述

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



DAL:

  public void SaveResource(Resource resource)
{
context.AddToResources(resource); //还应该添加子项。
context.SaveChanges();
}

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

返回资源;
}

服务:

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

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

parentResource.ToEdges.Add(inEdge);

repository.SaveResource(parentResource);
}

错误:ObjectStateManager中已经存在具有相同键的对象。现有对象处于未更改状态。一个对象只能在添加状态时再次添加到ObjectStateManager中。



图像:



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



我被告知这是向现有父母提交孩子的顺序:



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



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



EDMX文件:

 <?xml version =1.0encoding =utf-8?> 
< edmx:Edmx Version =2.0xmlns:edmx =http://schemas.microsoft.com/ado/2008/10/edmx>
<! - EF运行时内容 - >
< edmx:运行时>
<! - SSDL内容 - >
< edmx:StorageModels>
< Schema Namespace =XDbModel.StoreAlias =SelfProvider =System.Data.SqlClientProviderManifestToken =2008xmlns:store =http://schemas.microsoft.com/ado/ 2007/12 / edm / EntityStoreSchemaGeneratorxmlns =http://schemas.microsoft.com/ado/2009/02/edm/ssdl>
< EntityContainer Name =XDbModelStoreContainer>
< EntitySet Name =BundlesEntityType =XDbModel.Store.Bundlesstore:Type =TablesSchema =dbo/>
< EntitySet Name =CellProvidersEntityType =XDbModel.Store.CellProvidersstore:Type =TablesSchema =dbo/>
< EntitySet Name =注释EntityType =XDbModel.Store.Commentsstore:Type =TablesSchema =dbo/>
< EntitySet Name =LocationPointsEntityType =XDbModel.Store.LocationPointsstore:Type =TablesSchema =dbo/>
< EntitySet Name =LocationsEntityType =XDbModel.Store.Locationsstore:Type =TablesSchema =dbo/>
< EntitySet Name =OfferEntityType =XDbModel.Store.Offersstore:Type =TablesSchema =dbo/>
< EntitySet Name =PostBundleJunctionEntityType =XDbModel.Store.PostBundleJunctionstore:Type =TablesSchema =dbo/>
< EntitySet Name =PostMediaEntityType =XDbModel.Store.PostMediastore:Type =TablesSchema =dbo/>
< EntitySet Name =PostsEntityType =XDbModel.Store.Postsstore:Type =TablesSchema =dbo/>
< EntitySet Name =ResourceEdgesEntityType =XDbModel.Store.ResourceEdgesstore:Type =TablesSchema =dbo/>
< EntitySet Name =ResourceNamesEntityType =XDbModel.Store.ResourceNamesstore:Type =TablesSchema =dbo/>
< EntitySet Name =ResourcesEntityType =XDbModel.Store.Resourcesstore:Type =TablesSchema =dbo/>
< EntitySet Name =sysdiagramsEntityType =XDbModel.Store.sysdiagramsstore:Type =TablesSchema =dbo/>
< EntitySet Name =UsersEntityType =XDbModel.Store.Usersstore:Type =TablesSchema =dbo/>
< EntitySet Name =VotesEntityType =XDbModel.Store.Votesstore:Type =TablesSchema =dbo/>
< AssociationSet Name =FK_Comments_OffersAssociation =XDbModel.Store.FK_Comments_Offers>
< End Role =优惠EntitySet =优惠/>
< End Role =注释EntitySet =注释/>
< / AssociationSet>
< AssociationSet Name =FK_Comments_UsersAssociation =XDbModel.Store.FK_Comments_Users>
< End Role =UsersEntitySet =Users/>
< End Role =注释EntitySet =注释/>
< / AssociationSet>
< AssociationSet Name =FK_LocationPoints_LocationsAssociation =XDbModel.Store.FK_LocationPoints_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =LocationPointsEntitySet =LocationPoints/>
< / AssociationSet>
< AssociationSet Name =FK_PostBundleJunction_BundlesAssociation =XDbModel.Store.FK_PostBundleJunction_Bundles>
< End Role =BundlesEntitySet =Bundles/>
< End Role =PostBundleJunctionEntitySet =PostBundleJunction/>
< / AssociationSet>
< AssociationSet Name =FK_PostBundleJunction_PostsAssociation =XDbModel.Store.FK_PostBundleJunction_Posts>
< End Role =PostsEntitySet =Posts/>
< End Role =PostBundleJunctionEntitySet =PostBundleJunction/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_LocationsAssociation =XDbModel.Store.FK_Posts_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_ResourceNamesAssociation =XDbModel.Store.FK_Posts_ResourceNames>
< End Role =ResourceNamesEntitySet =ResourceNames/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_UsersAssociation =XDbModel.Store.FK_Posts_Users>
< End Role =UsersEntitySet =Users/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_ResourceEdges_ResourcesAssociation =XDbModel.Store.FK_ResourceEdges_Resources>
< End Role =资源EntitySet =资源/>
< End Role =ResourceEdgesEntitySet =ResourceEdges/>
< / AssociationSet>
< AssociationSet Name =FK_ResourceEdges_Resources1Association =XDbModel.Store.FK_ResourceEdges_Resources1>
< End Role =资源EntitySet =资源/>
< End Role =ResourceEdgesEntitySet =ResourceEdges/>
< / AssociationSet>
< AssociationSet Name =FK_ResourceNames_ResourcesAssociation =XDbModel.Store.FK_ResourceNames_Resources>
< End Role =资源EntitySet =资源/>
< End Role =ResourceNamesEntitySet =ResourceNames/>
< / AssociationSet>
< AssociationSet Name =FK_Users_LocationsAssociation =XDbModel.Store.FK_Users_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =UsersEntitySet =Users/>
< / AssociationSet>
< AssociationSet Name =FK_Votes_PostsAssociation =XDbModel.Store.FK_Votes_Posts>
< End Role =PostsEntitySet =Posts/>
< End Role =VotesEntitySet =Votes/>
< / AssociationSet>
< / EntityContainer>
< EntityType Name =Bundles>
< Key>
< PropertyRef Name =BundleId/>
< / Key>
< Property Name =BundleIdType =intNullable =false/>
< / EntityType>
< EntityType Name =CellProviders>
< Key>
< PropertyRef Name =CellProviderID/>
< / Key>
< Property Name =CellProviderIDType =uniqueidentifierNullable =false/>
< / EntityType>
< EntityType Name =注释>
< Key>
< PropertyRef Name =CommentID/>
< / Key>
< Property Name =CommentIDType =uniqueidentifierNullable =false/>
< Property Name =ParentPostIDType =uniqueidentifier/>
< Property Name =OfferPostIDType =uniqueidentifier/>
< Property Name =UserIDType =uniqueidentifierNullable =false/>
< / EntityType>
< EntityType Name =LocationPoints>
< Key>
< PropertyRef Name =LocationPointId/>
< / Key>
< Property Name =LocationPointIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =LatitudeType =floatNullable =false/>
< Property Name =LongitudeType =floatNullable =false/>
< Property Name =AltitudeType =floatNullable =false/>
< Property Name =CountType =intNullable =false/>
< Property Name =LocationIdType =intNullable =false/>
< / EntityType>
< EntityType Name =位置>
< Key>
< PropertyRef Name =LocationId/>
< / Key>
< Property Name =LocationIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =Address1Type =nvarcharNullable =falseMaxLength =200/>
< Property Name =Address2Type =nvarcharMaxLength =200/>
< Property Name =Address3Type =nvarcharMaxLength =200/>
< Property Name =StateType =nvarcharNullable =falseMaxLength =200/>
< Property Name =CountryType =nvarcharNullable =falseMaxLength =200/>
< / EntityType>
< EntityType Name =优惠>
< Key>
< PropertyRef Name =OfferID/>
< / Key>
< Property Name =OfferIDType =uniqueidentifierNullable =false/>
< Property Name =NeedOfferType =uniqueidentifierNullable =false/>
< Property Name =ProvisionOfferType =uniqueidentifierNullable =false/>
< / EntityType>
< EntityType Name =PostBundleJunction>
< Key>
< PropertyRef Name =BundleId/>
< PropertyRef Name =PostId/>
< / Key>
< Property Name =BundleIdType =intNullable =false/>
< Property Name =PostIdType =intNullable =false/>
< / EntityType>
< EntityType Name =PostMedia>
< Key>
< PropertyRef Name =MediaId/>
< / Key>
< Property Name =MediaIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =MediaExtType =nvarcharNullable =falseMaxLength =5/>
< Property Name =PostIdType =intNullable =false/>
< Property Name =SynthIdType =uniqueidentifier/>
< / EntityType>
< EntityType Name =Posts>
< Key>
< PropertyRef Name =PostId/>
< / Key>
< Property Name =PostIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =PosterIDType =uniqueidentifierNullable =false/>
< Property Name =BundleIdType =intNullable =false/>
< Property Name =LocationIdType =intNullable =false/>
< Property Name =TagsType =uniqueidentifier/>
< Property Name =QuanitityType =intNullable =false/>
< Property Name =DescriptionType =textNullable =false/>
< Property Name =ResourceNameIdType =intNullable =false/>
< Property Name =DateType =datetimeNullable =false/>
< / EntityType>
< EntityType Name =ResourceEdges>
< Key>
< PropertyRef Name =EdgeId/>
< / Key>
< Property Name =RankType =intNullable =false/>
< Property Name =EdgeIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =ToResourceIdType =intNullable =false/>
< Property Name =FromResourrceIdType =intNullable =false/>
< / EntityType>
< EntityType Name =ResourceNames>
< Key>
< PropertyRef Name =ResourceNameId/>
< / Key>
< Property Name =ResourceNameIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =NameType =nvarcharNullable =falseMaxLength =100/>
< Property Name =ResourceIdType =intNullable =false/>
< / EntityType>
< EntityType Name =资源>
< Key>
< PropertyRef Name =ResourceId/>
< / Key>
< Property Name =ResourceIdType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =DescriptionType =nvarcharMaxLength =50/>
< / EntityType>
< EntityType Name =sysdiagrams>
< Key>
< PropertyRef Name =diagram_id/>
< / Key>
< Property Name =nameType =nvarcharNullable =falseMaxLength =128/>
< Property Name =principal_idType =intNullable =false/>
< Property Name =diagram_idType =intNullable =falseStoreGeneratedPattern =Identity/>
< Property Name =versionType =int/>
< Property Name =definitionType =varbinary(max)/>
< / EntityType>
< EntityType Name =Users>
< Key>
< PropertyRef Name =UserID/>
< / Key>
< Property Name =UserIDType =uniqueidentifierNullable =false/>
< Property Name =LocationIdType =int/>
< / EntityType>
< EntityType Name =Votes>
< Key>
< PropertyRef Name =VoteId/>
< / Key>
< Property Name =VoteIdType =intNullable =false/>
< Property Name =VoterIdType =uniqueidentifierNullable =false/>
< Property Name =VoteContentType =intNullable =false/>
< Property Name =PostIdType =intNullable =false/>
< / EntityType>
<关联名称=FK_Comments_Offers>
< End Role =OfferType =XDbModel.Store.OffersMultiplicity =0..1/>
< End Role =注释Type =XDbModel.Store.CommentsMultiplicity =*/>
<参考证明>
<主体角色=优惠>
< PropertyRef Name =OfferID/>
< / Principal>
<从属角色=注释>
< PropertyRef Name =OfferPostID/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Comments_Users>
< End Role =UsersType =XDbModel.Store.UsersMultiplicity =1>
< OnDelete Action =Cascade/>
< / End>
< End Role =注释Type =XDbModel.Store.CommentsMultiplicity =*/>
<参考证明>
< Principal Role =Users>
< PropertyRef Name =UserID/>
< / Principal>
<从属角色=注释>
< PropertyRef Name =UserID/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_LocationPoints_Locations>
< End Role =LocationsType =XDbModel.Store.LocationsMultiplicity =1>
< OnDelete Action =Cascade/>
< / End>
< End Role =LocationPointsType =XDbModel.Store.LocationPointsMultiplicity =*/>
<参考证明>
<主体角色=位置>
< PropertyRef Name =LocationId/>
< / Principal>
<从属角色=位置点>
< PropertyRef Name =LocationId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_PostBundleJunction_Bundles>
< End Role =BundlesType =XDbModel.Store.BundlesMultiplicity =1/>
< End Role =PostBundleJunctionType =XDbModel.Store.PostBundleJunctionMultiplicity =*/>
<参考证明>
< Principal Role =Bundles>
< PropertyRef Name =BundleId/>
< / Principal>
<从属角色=PostBundleJunction>
< PropertyRef Name =BundleId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_PostBundleJunction_Posts>
< End Role =PostsType =XDbModel.Store.PostsMultiplicity =1/>
< End Role =PostBundleJunctionType =XDbModel.Store.PostBundleJunctionMultiplicity =*/>
<参考证明>
< Principal Role =Posts>
< PropertyRef Name =PostId/>
< / Principal>
<从属角色=PostBundleJunction>
< PropertyRef Name =PostId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Posts_Locations>
< End Role =LocationsType =XDbModel.Store.LocationsMultiplicity =1/>
< End Role =PostsType =XDbModel.Store.PostsMultiplicity =*/>
<参考证明>
<主体角色=位置>
< PropertyRef Name =LocationId/>
< / Principal>
< Dependent Role =Posts>
< PropertyRef Name =LocationId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Posts_ResourceNames>
< End Role =ResourceNamesType =XDbModel.Store.ResourceNamesMultiplicity =1/>
< End Role =PostsType =XDbModel.Store.PostsMultiplicity =*/>
<参考证明>
< Principal Role =ResourceNames>
< PropertyRef Name =ResourceNameId/>
< / Principal>
< Dependent Role =Posts>
< PropertyRef Name =ResourceNameId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Posts_Users>
< End Role =UsersType =XDbModel.Store.UsersMultiplicity =1/>
< End Role =PostsType =XDbModel.Store.PostsMultiplicity =*/>
<参考证明>
< Principal Role =Users>
< PropertyRef Name =UserID/>
< / Principal>
< Dependent Role =Posts>
< PropertyRef Name =PosterID/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_ResourceEdges_Resources>
< End Role =ResourcesType =XDbModel.Store.ResourcesMultiplicity =1/>
< End Role =ResourceEdgesType =XDbModel.Store.ResourceEdgesMultiplicity =*/>
<参考证明>
< Principal Role =资源>
< PropertyRef Name =ResourceId/>
< / Principal>
<从属角色=ResourceEdges>
< PropertyRef Name =ToResourceId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_ResourceEdges_Resources1>
< End Role =ResourcesType =XDbModel.Store.ResourcesMultiplicity =1/>
< End Role =ResourceEdgesType =XDbModel.Store.ResourceEdgesMultiplicity =*/>
<参考证明>
< Principal Role =资源>
< PropertyRef Name =ResourceId/>
< / Principal>
<从属角色=ResourceEdges>
< PropertyRef Name =FromResourrceId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_ResourceNames_Resources>
< End Role =ResourcesType =XDbModel.Store.ResourcesMultiplicity =1>
< OnDelete Action =Cascade/>
< / End>
< End Role =ResourceNamesType =XDbModel.Store.ResourceNamesMultiplicity =*/>
<参考证明>
< Principal Role =资源>
< PropertyRef Name =ResourceId/>
< / Principal>
<从属角色=ResourceNames>
< PropertyRef Name =ResourceId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Users_Locations>
< End Role =LocationsType =XDbModel.Store.LocationsMultiplicity =0..1>
< OnDelete Action =Cascade/>
< / End>
< End Role =UsersType =XDbModel.Store.UsersMultiplicity =*/>
<参考证明>
<主体角色=位置>
< PropertyRef Name =LocationId/>
< / Principal>
<从属角色=用户>
< PropertyRef Name =LocationId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
<关联名称=FK_Votes_Posts>
< End Role =PostsType =XDbModel.Store.PostsMultiplicity =1>
< OnDelete Action =Cascade/>
< / End>
< End Role =VotesType =XDbModel.Store.VotesMultiplicity =*/>
<参考证明>
< Principal Role =Posts>
< PropertyRef Name =PostId/>
< / Principal>
<从属角色=投票>
< PropertyRef Name =PostId/>
< / Dependent>
< / ReferentialConstraint>
< / Association>
< / Schema>
< / edmx:StorageModels>
<! - CSDL内容 - >
< edmx:ConceptualModels>
< Schema Namespace =XDbModelAlias =Selfxmlns:annotation =http://schemas.microsoft.com/ado/2009/02/edm/annotationxmlns =http:// schemas .microsoft.com / ADO / 2008/09 / EDM>
< EntityContainer Name =XDbEntities注释:LazyLoadingEnabled =true>
< EntitySet Name =BundlesEntityType =XDbModel.Bundle/>
< EntitySet Name =CellProvidersEntityType =XDbModel.CellProvider/>
< EntitySet Name =注释EntityType =XDbModel.Comment/>
< EntitySet Name =LocationPointsEntityType =XDbModel.LocationPoint/>
< EntitySet Name =LocationsEntityType =XDbModel.Location/>
< EntitySet Name =OfferEntityType =XDbModel.Offer/>
< EntitySet Name =PostMediasEntityType =XDbModel.PostMedia/>
< EntitySet Name =PostsEntityType =XDbModel.Post/>
< EntitySet Name =ResourceEdgesEntityType =XDbModel.ResourceEdge/>
< EntitySet Name =ResourceNamesEntityType =XDbModel.ResourceName/>
< EntitySet Name =ResourcesEntityType =XDbModel.Resource/>
< EntitySet Name =sysdiagramsEntityType =XDbModel.sysdiagram/>
< EntitySet Name =UsersEntityType =XDbModel.User/>
< EntitySet Name =VotesEntityType =XDbModel.Vote/>
< AssociationSet Name =FK_Comments_OffersAssociation =XDbModel.FK_Comments_Offers>
< End Role =优惠EntitySet =优惠/>
< End Role =注释EntitySet =注释/>
< / AssociationSet>
< AssociationSet Name =FK_Comments_UsersAssociation =XDbModel.FK_Comments_Users>
< End Role =UsersEntitySet =Users/>
< End Role =注释EntitySet =注释/>
< / AssociationSet>
< AssociationSet Name =FK_LocationPoints_LocationsAssociation =XDbModel.FK_LocationPoints_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =LocationPointsEntitySet =LocationPoints/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_LocationsAssociation =XDbModel.FK_Posts_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_Users_LocationsAssociation =XDbModel.FK_Users_Locations>
< End Role =LocationsEntitySet =Locations/>
< End Role =UsersEntitySet =Users/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_ResourceNamesAssociation =XDbModel.FK_Posts_ResourceNames>
< End Role =ResourceNamesEntitySet =ResourceNames/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_Posts_UsersAssociation =XDbModel.FK_Posts_Users>
< End Role =UsersEntitySet =Users/>
< End Role =PostsEntitySet =Posts/>
< / AssociationSet>
< AssociationSet Name =FK_Votes_PostsAssociation =XDbModel.FK_Votes_Posts>
< End Role =PostsEntitySet =Posts/>
< End Role =VotesEntitySet =Votes/>
< / AssociationSet>
< AssociationSet Name =FK_ResourceEdges_ResourcesAssociation =XDbModel.FK_ResourceEdges_Resources>
< End Role =资源EntitySet =资源/>
< End Role =ResourceEdgesEntitySet =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();
}


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:

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

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天全站免登陆