"An item with the same key has already been added" error when calling Context AddObject - entity-framework-4

Can somebody please help me with this error. I have an ASP.NET WebApp, within the Entity Model I have an entity called tb_AdminUser, with a UserID property as GUID. This is also the Entity Key. The entity is not in any relationship with any other entity. I get the error when calling the AddObject() function.
In my code I call the following...
SQL2008R2_824852_leapdbEntities temp = new SQL2008R2_824852_leapdbEntities();
tb_AdminUser au = new tb_AdminUser();
au.UserID = Guid.NewGuid();
au.Username = "TEST";
au.Password = "pete#webinspired.net";
au.LockedOut = false;
au.Surname = "Feehan";
au.Forename = "Pete";
temp.tb_AdminUser.AddObject(au); //error occurs here
temp.SaveChanges();
The EDMX XML Code is as follows
<EntityType Name="tb_AdminUser">
<Key>
<PropertyRef Name="UserID" />
</Key>
<Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
<Property Name="Forename" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="Surname" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="Username" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="Password" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="LockedOut" Type="bit" Nullable="false" />
</EntityType>

Related

The function import is missing in the metadata of odata (odata created via CDS)

I want to create a function import in odata for use in sapui5 application. I create CDS view, add BOPF, create action SEND_MAIL, add annotation to odata. But function import tag is still missing in the metadata, i dont know why. And Exporting type in action inactive. Determination working, action not. What i do wrong?
СDS View:
#AbapCatalog.sqlViewName: 'ZGUT_FIORY_VW'
#AbapCatalog.compiler.compareFilter: true
#VDM.viewType: #COMPOSITE
#AbapCatalog.preserveKey: true
#AccessControl.authorizationCheck: #CHECK
#EndUserText.label: 'Test cds fiori list report'
// BOPF CRUD
#Metadata.allowExtensions: true
#ObjectModel:{
modelCategory: #BUSINESS_OBJECT,
compositionRoot: true,
transactionalProcessingEnabled: true,
createEnabled: true,
updateEnabled: true,
deleteEnabled: true,
writeActivePersistence: 'ZGUT_TEST_UI5',
semanticKey: ['id'],
representativeKey: 'id'
}
#OData:{
publish:true
}
#UI.headerInfo: {
typeName : 'Тестовое приложение fiori', // Заголовок списка
typeNamePlural: 'Тестовое приложение fiori' ,
title.value: 'Title', // Заголовок детальной страницы
description.value: 'bukrs_txt'
}
#Search.searchable: true
define view ZGUT_CDS_FIORY as select from zgut_test_ui5
association [1..1] to t001 as _bukrs on zgut_test_ui5.bukrs = _bukrs.bukrs
association [0..*] to ZGUT_CHART_AMOUNT as _chartamount on zgut_test_ui5.curr = _chartamount.Curr
association [1..1] to ZGUT_CHART_AMOUNT as _chartamount2 on zgut_test_ui5.id = _chartamount2.Ids
{
#UI.lineItem:[
{position: 30},
{ type: #FOR_ACTION, dataAction: 'BOPF:SEND_MAIL', position: 1, label: 'Send mail' }
]
#ObjectModel.mandatory: true
key id as Id,
// Another fields
}
BOPF:
Action:
Action class:
Metadata (i dont see here any function import, i dont know why) /sap/opu/odata/sap/ZGUT_CDS_FIORY_CDS/$metadata:
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/Protocols/SAPData" Version="1.0">
<edmx:Reference xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Uri="http://host:50000/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/Vocabularies(TechnicalName='%2FIWBEP%2FVOC_COMMON',Version='0001',SAP__Origin='LOCAL')/$value">
<edmx:Include Namespace="com.sap.vocabularies.Common.v1" Alias="Common"/>
</edmx:Reference>
<edmx:DataServices m:DataServiceVersion="2.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="ZGUT_CDS_FIORY_CDS" xml:lang="ru" sap:schema-version="1">
<EntityType Name="ZGUT_CDS_FIORYType" sap:label="Test cds fiori list report" sap:content-version="1">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Send_mail_ac" Type="Edm.Boolean" sap:label="Dyn. Action Control" sap:creatable="false" sap:updatable="false" sap:sortable="false" sap:filterable="false"/>
<Property Name="Id" Type="Edm.String" Nullable="false" MaxLength="6" sap:display-format="NonNegative" sap:label="Номер"/>
<Property Name="Descr" Type="Edm.String" sap:label="Описание"/>
<Property Name="target_amount" Type="Edm.String" MaxLength="6" sap:display-format="NonNegative" sap:label="Количество"/>
<Property Name="Title" Type="Edm.String" MaxLength="255" sap:label="Заголовок"/>
<Property Name="Bukrs" Type="Edm.String" MaxLength="4" sap:display-format="UpperCase" sap:label="БЕ"/>
<Property Name="bukrs_txt" Type="Edm.String" MaxLength="25" sap:label="Название фирмы" sap:creatable="false" sap:updatable="false"/>
<Property Name="Ab" Type="Edm.DateTime" Precision="0" sap:display-format="Date" sap:label="Дата начала"/>
<Property Name="Bis" Type="Edm.DateTime" Precision="0" sap:display-format="Date" sap:label="Дата конца"/>
<Property Name="Price" Type="Edm.Decimal" Precision="11" Scale="3" sap:label="Цена"/>
<Property Name="Curr" Type="Edm.String" MaxLength="3" sap:display-format="UpperCase" sap:label="Валюта"/>
<Property Name="Amount" Type="Edm.String" MaxLength="6" sap:display-format="NonNegative" sap:label="Количество"/>
<NavigationProperty Name="to_chartamount" Relationship="ZGUT_CDS_FIORY_CDS.assoc_17D58845CCECEF801599D15F5569B65F" FromRole="FromRole_assoc_17D58845CCECEF801599D15F5569B65F" ToRole="ToRole_assoc_17D58845CCECEF801599D15F5569B65F"/>
<NavigationProperty Name="to_chartamount2" Relationship="ZGUT_CDS_FIORY_CDS.assoc_84AF2B5FF9693A0A2F011CFC2072B09A" FromRole="FromRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A" ToRole="ToRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A"/>
</EntityType>
<EntityType Name="ZGUT_CHART_AMOUNTType" sap:semantics="aggregate" sap:label="ZGUT_CHART_AMOUNT" sap:content-version="1">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Type="Edm.String" Nullable="false"/>
<Property Name="Ids" Type="Edm.String" MaxLength="6" sap:aggregation-role="dimension" sap:display-format="NonNegative" sap:label="Номер"/>
<Property Name="Amounts" Type="Edm.String" MaxLength="6" sap:aggregation-role="measure" sap:display-format="NonNegative" sap:label="Количество" sap:filterable="false"/>
<Property Name="target_amount" Type="Edm.String" MaxLength="6" sap:aggregation-role="dimension" sap:display-format="NonNegative" sap:label="Количество"/>
<Property Name="Price" Type="Edm.Decimal" Precision="11" Scale="3" sap:aggregation-role="dimension" sap:label="Цена"/>
<Property Name="Curr" Type="Edm.String" MaxLength="3" sap:aggregation-role="dimension" sap:display-format="UpperCase" sap:label="Валюта"/>
</EntityType>
<Association Name="assoc_84AF2B5FF9693A0A2F011CFC2072B09A" sap:content-version="1">
<End Type="ZGUT_CDS_FIORY_CDS.ZGUT_CDS_FIORYType" Multiplicity="1" Role="FromRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A"/>
<End Type="ZGUT_CDS_FIORY_CDS.ZGUT_CHART_AMOUNTType" Multiplicity="1" Role="ToRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A"/>
</Association>
<Association Name="assoc_17D58845CCECEF801599D15F5569B65F" sap:content-version="1">
<End Type="ZGUT_CDS_FIORY_CDS.ZGUT_CDS_FIORYType" Multiplicity="1" Role="FromRole_assoc_17D58845CCECEF801599D15F5569B65F"/>
<End Type="ZGUT_CDS_FIORY_CDS.ZGUT_CHART_AMOUNTType" Multiplicity="*" Role="ToRole_assoc_17D58845CCECEF801599D15F5569B65F"/>
</Association>
<EntityContainer Name="ZGUT_CDS_FIORY_CDS_Entities" m:IsDefaultEntityContainer="true" sap:supported-formats="atom json xlsx">
<EntitySet Name="ZGUT_CDS_FIORY" EntityType="ZGUT_CDS_FIORY_CDS.ZGUT_CDS_FIORYType" sap:searchable="true" sap:content-version="1"/>
<EntitySet Name="ZGUT_CHART_AMOUNT" EntityType="ZGUT_CDS_FIORY_CDS.ZGUT_CHART_AMOUNTType" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:content-version="1"/>
<AssociationSet Name="assoc_17D58845CCECEF801599D15F5569B65F" Association="ZGUT_CDS_FIORY_CDS.assoc_17D58845CCECEF801599D15F5569B65F" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:content-version="1">
<End EntitySet="ZGUT_CDS_FIORY" Role="FromRole_assoc_17D58845CCECEF801599D15F5569B65F"/>
<End EntitySet="ZGUT_CHART_AMOUNT" Role="ToRole_assoc_17D58845CCECEF801599D15F5569B65F"/>
</AssociationSet>
<AssociationSet Name="assoc_84AF2B5FF9693A0A2F011CFC2072B09A" Association="ZGUT_CDS_FIORY_CDS.assoc_84AF2B5FF9693A0A2F011CFC2072B09A" sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:content-version="1">
<End EntitySet="ZGUT_CDS_FIORY" Role="FromRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A"/>
<End EntitySet="ZGUT_CHART_AMOUNT" Role="ToRole_assoc_84AF2B5FF9693A0A2F011CFC2072B09A"/>
</AssociationSet>
</EntityContainer>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="self" href="http://host:50000/sap/opu/odata/sap/ZGUT_CDS_FIORY_CDS/$metadata"/>
<atom:link xmlns:atom="http://www.w3.org/2005/Atom" rel="latest-version" href="http://host:50000/sap/opu/odata/sap/ZGUT_CDS_FIORY_CDS/$metadata"/>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
I solved the issue by choosing the exporting type in the actions list and reactivate the bopf

OData Shortened Key Predicate Support

I'm building a OData service using the ODataLib v7.1.1 bits from Microsoft.OData.Core (et al). I'm having trouble using the shortened key predicate URL format with ODataUriParser.ParsePath(). The (relative) URL "Company('Comp1')/Customer(1)" throws an exception "The number of keys specified in the URI does not match number of key properties for the resource 'Test.Customer'".
The model is very simple and I can't see why this isn't working. Other formats for the URLs work ("Company('Comp1')", "Customer(CompanyId='COMP1',CustNum=1)").
I've put the model builder code below along with the $metadata output. Any help would be appreciated. Thanks!
-Erik
var result = new EdmModel();
var company = new EdmEntityType("Test", "Company");
var companyKey1 = company.AddStructuralProperty("CompanyId", EdmPrimitiveTypeKind.String, false);
company.AddKeys(
companyKey1
);
result.AddElement(company);
var customer = new EdmEntityType(NS, "Customer");
var customerKey1 = customer.AddStructuralProperty("CompanyId", EdmPrimitiveTypeKind.String, false);
var customerKey2 = customer.AddStructuralProperty("CustNum", EdmPrimitiveTypeKind.Int32, false);
customer.AddKeys(
customerKey1,
customerKey2
);
result.AddElement(customer);
var navCompanyCustomer = company.AddUnidirectionalNavigation(
new EdmNavigationPropertyInfo()
{
ContainsTarget = true,
Name = "Customer",
Target = customer,
TargetMultiplicity = EdmMultiplicity.Many
});
var customerRefToCompany = customer.AddUnidirectionalNavigation(
new EdmNavigationPropertyInfo()
{
ContainsTarget = false,
Name = "Company",
Target = company,
TargetMultiplicity = EdmMultiplicity.One,
DependentProperties = new[] { customerKey1 },
PrincipalProperties = new[] { companyKey1 }
});
var container = new EdmEntityContainer("Test", "DefaultContainer");
result.AddElement(container);
container.AddEntitySet("Company", company);
container.AddEntitySet("Customer", customer);
return result;
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="Test" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Company">
<Key>
<PropertyRef Name="CompanyId" />
</Key>
<Property Name="CompanyId" Type="Edm.String" Nullable="false" />
<NavigationProperty Name="Customer" Type="Collection(Test.Customer)" ContainsTarget="true" />
</EntityType>
<EntityType Name="Customer">
<Key>
<PropertyRef Name="CompanyId" />
<PropertyRef Name="CustNum" />
</Key>
<Property Name="CompanyId" Type="Edm.String" Nullable="false" />
<Property Name="CustNum" Type="Edm.Int32" Nullable="false" />
<NavigationProperty Name="Company" Type="Test.Company" Nullable="false">
<ReferentialConstraint Property="CompanyId" ReferencedProperty="CompanyId" />
</NavigationProperty>
</EntityType>
<EntityContainer Name="DefaultContainer">
<EntitySet Name="Company" EntityType="Test.Company" />
<EntitySet Name="Customer" EntityType="Test.Customer" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
I figured it out. I had the Dependent / Principal properties reversed (corrected code is below). One of the big debates (years ago) was which way the arrow should point on an RI declaration in entity-relationship model (i.e. which side is dependent). I must of been on the losing side of that one. ;)
var navCompanyCustomer = company.AddUnidirectionalNavigation(
new EdmNavigationPropertyInfo()
{
ContainsTarget = true,
Name = "Customer",
Target = customer,
TargetMultiplicity = EdmMultiplicity.Many,
DependentProperties = new[] { companyKey1 },
PrincipalProperties = new[] { customerKey1 }
});

RESTier OData Function that support $expand and $filter

With the help of the RESTier team, I manage to create an RESTier function that return a list of my entity.
Here's the code
protected EdmModel OnModelExtending(EdmModel model)
{
var ns = model.DeclaredNamespaces.First(); // PointLoc.Data
var entityContainer = (EdmEntityContainer) model.EntityContainer;
var locationEntityType = (IEdmEntityType) model.FindDeclaredType(ns + "." + "Location");
var locationEntitySet = entityContainer.FindEntitySet("Locations");
var locationEntityTypeReference = new EdmEntityTypeReference(locationEntityType, false);
var locationEntityCollection = EdmCoreModel.GetCollection(locationEntityTypeReference);
var ambilLocationsByMarketId = new EdmFunction(ns, "AmbilLocationsByMarketId", locationEntityCollection, false, null, true);
model.AddElement(ambilLocationsByMarketId);
entityContainer.AddFunctionImport("AmbilLocationsByMarketId", ambilLocationsByMarketId,
new EdmEntitySetReferenceExpression(locationEntitySet));
return model;
}
And here's my implementation on the Controller
[HttpGet]
[EnableQuery]
[ODataRoute("AmbilLocationsByMarketId")]
public IQueryable<Location> AmbilLocationsByMarketId()
{
var locations = DbContext.Locations.Where(l => l.Name.Contains("Hotel")).Select(l => l);
return locations;
}
It works fine returning the list of data with i send a HTTP GET to
http://localhost:21922/odata/AmbilLocationsByMarketId
but when I try to add $expand or $filter, It's not working.
http://localhost:21922/odata/AmbilLocationsByMarketId?$expand=Category
I'm Error that reads like
{
"error": {
"code": "",
"message": "An error has occurred.",
"innererror": {
"message": "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.",
"type": "System.InvalidOperationException",
"stacktrace": "",
"internalexception": {
"message": "'DbQuery`1' cannot be serialized using the ODataMediaTypeFormatter.",
"type": "System.Runtime.Serialization.SerializationException",
"stacktrace": " at System.Web.OData.Formatter.ODataMediaTypeFormatter.GetSerializer(Type type, Object value, IEdmModel model, ODataSerializerProvider serializerProvider)\\\r\\\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)\\\r\\\n at System.Web.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\\\r\\\n--- End of stack trace from previous location where exception was thrown ---\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\\r\\\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\\\r\\\n at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()"
}
}
}
}
Here's my metadata
<EntityType Name="Location">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true" />
</Property>
<Property Name="Name" Type="Edm.String" Nullable="false" MaxLength="500" />
<Property Name="Address" Type="Edm.String" Nullable="false" MaxLength="2000" />
<Property Name="City" Type="Edm.String" MaxLength="500" />
<Property Name="Postcode" Type="Edm.String" MaxLength="100" />
<Property Name="Phone" Type="Edm.String" MaxLength="50" />
<Property Name="Email" Type="Edm.String" MaxLength="200" />
<Property Name="Latitude" Type="Edm.Decimal" Nullable="false" Precision="9" Scale="6" />
<Property Name="Longitude" Type="Edm.Decimal" Nullable="false" Precision="9" Scale="6" />
<Property Name="Street" Type="Edm.String" MaxLength="1000" />
<Property Name="UpVotes" Type="Edm.Int32" Nullable="false" />
<Property Name="DownVotes" Type="Edm.Int32" Nullable="false" />
<Property Name="CategoryId" Type="Edm.Int32" Nullable="false" />
<Property Name="StatusId" Type="Edm.Int32" Nullable="false" />
<Property Name="StateId" Type="Edm.Int32" Nullable="false" />
<Property Name="TitleSlug" Type="Edm.String" MaxLength="200" />
<NavigationProperty Name="Category" Type="PointLoc.Data.Category" Nullable="false" Partner="Locations">
<ReferentialConstraint Property="CategoryId" ReferencedProperty="Id" />
</NavigationProperty>
</EntityType>
and here's the EntityContainer
<EntityContainer Name="DatabaseContext">
<EntitySet Name="Categories" EntityType="PointLoc.Data.Category">
<NavigationPropertyBinding Path="Locations" Target="Locations" />
</EntitySet>
<EntitySet Name="Locations" EntityType="PointLoc.Data.Location">
<NavigationPropertyBinding Path="Accesses" Target="Accesses" />
<NavigationPropertyBinding Path="Category" Target="Categories" />
<NavigationPropertyBinding Path="Contents" Target="Contents" />
<NavigationPropertyBinding Path="State" Target="States" />
<NavigationPropertyBinding Path="Status" Target="Status" />
<NavigationPropertyBinding Path="LocationMarketMaps" Target="LocationMarketMaps" />
<NavigationPropertyBinding Path="LocationTagMaps" Target="LocationTagMaps" />
</EntitySet>
<FunctionImport Name="AmbilLocationsByMarketId" Function="PointLoc.Data.AmbilLocationsByMarketId" EntitySet="Locations" />
</EntityContainer>

Tiles attributes not found

I use tiles 2.0
In my tiles-defs.xml i have
<definition name="user" extends="baseLayout">
<put-attribute name="title" value="Share Admin user" />
<put-attribute name="body" value="user.body" />
</definition>
<definition name="user.body" template="/WEB-INF/view/user.jsp">
<put-attribute name="editingUserForm" value="/WEB-INF/view/userEditingModelFormModal.jsp"/>
</definition>
in my user.jsp i have
<tiles:insertAttribute name="editingUserForm" />
when i load my user page, i get
org.apache.tiles.template.NoSuchAttributeException: Attribute 'editingUserForm' not found.
/WEB-INF/tiles/tiles-defs.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="order" value="1" />
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
#RequestMapping(method = RequestMethod.GET, value = "/admin/editingUser")
public String showAjaxEditingUser(Model model, #RequestParam("username") String userName) {
UserBean userBean = userBeanMap.get(userName);
model.addAttribute("editingUser", userBean);
return "editingUserFormBodyPart";
}

Add child to existing parent record in entity framework

My relationship between the parent and child is that they are connected by an edge. It is similiar to a directed graph structure.
DAL:
public void SaveResource(Resource resource)
{
context.AddToResources(resource); //Should also add children.
context.SaveChanges();
}
public Resource GetResource(int resourceId)
{
var resource = (from r in context.Resources
.Include("ToEdges").Include("FromEdges")
where r.ResourceId == resourceId
select r).SingleOrDefault();
return resource;
}
Service:
public void AddChildResource(int parentResourceId, Resource childResource)
{
Resource parentResource = repository.GetResource(parentResourceId);
ResourceEdge inEdge = new ResourceEdge();
inEdge.ToResource = childResource;
parentResource.ToEdges.Add(inEdge);
repository.SaveResource(parentResource);
}
Error: An object with the same key already exists in the ObjectStateManager. The existing object is in the Unchanged state. An object can only be added to the ObjectStateManager again if it is in the added state.
Image:
I have been told this is the sequence in submitting a child to an already existing parent:
Get parent -> Attach Child to parent -> submit parent.
That is the sequence I used. The code above is extracted from an ASP.NET MVC 2 application using the repository pattern.
EDMX file:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="XDbModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="XDbModelStoreContainer">
<EntitySet Name="Bundles" EntityType="XDbModel.Store.Bundles" store:Type="Tables" Schema="dbo" />
<EntitySet Name="CellProviders" EntityType="XDbModel.Store.CellProviders" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Comments" EntityType="XDbModel.Store.Comments" store:Type="Tables" Schema="dbo" />
<EntitySet Name="LocationPoints" EntityType="XDbModel.Store.LocationPoints" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Locations" EntityType="XDbModel.Store.Locations" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Offers" EntityType="XDbModel.Store.Offers" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PostBundleJunction" EntityType="XDbModel.Store.PostBundleJunction" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PostMedia" EntityType="XDbModel.Store.PostMedia" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Posts" EntityType="XDbModel.Store.Posts" store:Type="Tables" Schema="dbo" />
<EntitySet Name="ResourceEdges" EntityType="XDbModel.Store.ResourceEdges" store:Type="Tables" Schema="dbo" />
<EntitySet Name="ResourceNames" EntityType="XDbModel.Store.ResourceNames" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Resources" EntityType="XDbModel.Store.Resources" store:Type="Tables" Schema="dbo" />
<EntitySet Name="sysdiagrams" EntityType="XDbModel.Store.sysdiagrams" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Users" EntityType="XDbModel.Store.Users" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Votes" EntityType="XDbModel.Store.Votes" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_Comments_Offers" Association="XDbModel.Store.FK_Comments_Offers">
<End Role="Offers" EntitySet="Offers" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_Comments_Users" Association="XDbModel.Store.FK_Comments_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.Store.FK_LocationPoints_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="LocationPoints" EntitySet="LocationPoints" />
</AssociationSet>
<AssociationSet Name="FK_PostBundleJunction_Bundles" Association="XDbModel.Store.FK_PostBundleJunction_Bundles">
<End Role="Bundles" EntitySet="Bundles" />
<End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
</AssociationSet>
<AssociationSet Name="FK_PostBundleJunction_Posts" Association="XDbModel.Store.FK_PostBundleJunction_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Locations" Association="XDbModel.Store.FK_Posts_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.Store.FK_Posts_ResourceNames">
<End Role="ResourceNames" EntitySet="ResourceNames" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Users" Association="XDbModel.Store.FK_Posts_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.Store.FK_ResourceEdges_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.Store.FK_ResourceEdges_Resources1">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.Store.FK_ResourceNames_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceNames" EntitySet="ResourceNames" />
</AssociationSet>
<AssociationSet Name="FK_Users_Locations" Association="XDbModel.Store.FK_Users_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Users" EntitySet="Users" />
</AssociationSet>
<AssociationSet Name="FK_Votes_Posts" Association="XDbModel.Store.FK_Votes_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="Votes" EntitySet="Votes" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Bundles">
<Key>
<PropertyRef Name="BundleId" />
</Key>
<Property Name="BundleId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="CellProviders">
<Key>
<PropertyRef Name="CellProviderID" />
</Key>
<Property Name="CellProviderID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="Comments">
<Key>
<PropertyRef Name="CommentID" />
</Key>
<Property Name="CommentID" Type="uniqueidentifier" Nullable="false" />
<Property Name="ParentPostID" Type="uniqueidentifier" />
<Property Name="OfferPostID" Type="uniqueidentifier" />
<Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="LocationPoints">
<Key>
<PropertyRef Name="LocationPointId" />
</Key>
<Property Name="LocationPointId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Latitude" Type="float" Nullable="false" />
<Property Name="Longitude" Type="float" Nullable="false" />
<Property Name="Altitude" Type="float" Nullable="false" />
<Property Name="Count" Type="int" Nullable="false" />
<Property Name="LocationId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="Locations">
<Key>
<PropertyRef Name="LocationId" />
</Key>
<Property Name="LocationId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Address1" Type="nvarchar" Nullable="false" MaxLength="200" />
<Property Name="Address2" Type="nvarchar" MaxLength="200" />
<Property Name="Address3" Type="nvarchar" MaxLength="200" />
<Property Name="State" Type="nvarchar" Nullable="false" MaxLength="200" />
<Property Name="Country" Type="nvarchar" Nullable="false" MaxLength="200" />
</EntityType>
<EntityType Name="Offers">
<Key>
<PropertyRef Name="OfferID" />
</Key>
<Property Name="OfferID" Type="uniqueidentifier" Nullable="false" />
<Property Name="NeedOffer" Type="uniqueidentifier" Nullable="false" />
<Property Name="ProvisionOffer" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="PostBundleJunction">
<Key>
<PropertyRef Name="BundleId" />
<PropertyRef Name="PostId" />
</Key>
<Property Name="BundleId" Type="int" Nullable="false" />
<Property Name="PostId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="PostMedia">
<Key>
<PropertyRef Name="MediaId" />
</Key>
<Property Name="MediaId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="MediaExt" Type="nvarchar" Nullable="false" MaxLength="5" />
<Property Name="PostId" Type="int" Nullable="false" />
<Property Name="SynthId" Type="uniqueidentifier" />
</EntityType>
<EntityType Name="Posts">
<Key>
<PropertyRef Name="PostId" />
</Key>
<Property Name="PostId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="PosterID" Type="uniqueidentifier" Nullable="false" />
<Property Name="BundleId" Type="int" Nullable="false" />
<Property Name="LocationId" Type="int" Nullable="false" />
<Property Name="Tags" Type="uniqueidentifier" />
<Property Name="Quanitity" Type="int" Nullable="false" />
<Property Name="Description" Type="text" Nullable="false" />
<Property Name="ResourceNameId" Type="int" Nullable="false" />
<Property Name="Date" Type="datetime" Nullable="false" />
</EntityType>
<EntityType Name="ResourceEdges">
<Key>
<PropertyRef Name="EdgeId" />
</Key>
<Property Name="Rank" Type="int" Nullable="false" />
<Property Name="EdgeId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ToResourceId" Type="int" Nullable="false" />
<Property Name="FromResourrceId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="ResourceNames">
<Key>
<PropertyRef Name="ResourceNameId" />
</Key>
<Property Name="ResourceNameId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="ResourceId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="Resources">
<Key>
<PropertyRef Name="ResourceId" />
</Key>
<Property Name="ResourceId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Description" Type="nvarchar" MaxLength="50" />
</EntityType>
<EntityType Name="sysdiagrams">
<Key>
<PropertyRef Name="diagram_id" />
</Key>
<Property Name="name" Type="nvarchar" Nullable="false" MaxLength="128" />
<Property Name="principal_id" Type="int" Nullable="false" />
<Property Name="diagram_id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="version" Type="int" />
<Property Name="definition" Type="varbinary(max)" />
</EntityType>
<EntityType Name="Users">
<Key>
<PropertyRef Name="UserID" />
</Key>
<Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
<Property Name="LocationId" Type="int" />
</EntityType>
<EntityType Name="Votes">
<Key>
<PropertyRef Name="VoteId" />
</Key>
<Property Name="VoteId" Type="int" Nullable="false" />
<Property Name="VoterId" Type="uniqueidentifier" Nullable="false" />
<Property Name="VoteContent" Type="int" Nullable="false" />
<Property Name="PostId" Type="int" Nullable="false" />
</EntityType>
<Association Name="FK_Comments_Offers">
<End Role="Offers" Type="XDbModel.Store.Offers" Multiplicity="0..1" />
<End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Offers">
<PropertyRef Name="OfferID" />
</Principal>
<Dependent Role="Comments">
<PropertyRef Name="OfferPostID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Comments_Users">
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Users">
<PropertyRef Name="UserID" />
</Principal>
<Dependent Role="Comments">
<PropertyRef Name="UserID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_LocationPoints_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="LocationPoints" Type="XDbModel.Store.LocationPoints" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="LocationPoints">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_PostBundleJunction_Bundles">
<End Role="Bundles" Type="XDbModel.Store.Bundles" Multiplicity="1" />
<End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Bundles">
<PropertyRef Name="BundleId" />
</Principal>
<Dependent Role="PostBundleJunction">
<PropertyRef Name="BundleId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_PostBundleJunction_Posts">
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1" />
<End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Posts">
<PropertyRef Name="PostId" />
</Principal>
<Dependent Role="PostBundleJunction">
<PropertyRef Name="PostId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_ResourceNames">
<End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="ResourceNames">
<PropertyRef Name="ResourceNameId" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="ResourceNameId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_Users">
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Users">
<PropertyRef Name="UserID" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="PosterID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceEdges_Resources">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
<End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceEdges">
<PropertyRef Name="ToResourceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceEdges_Resources1">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
<End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceEdges">
<PropertyRef Name="FromResourrceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceNames_Resources">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceNames">
<PropertyRef Name="ResourceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Users_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="0..1">
<OnDelete Action="Cascade" />
</End>
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="Users">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Votes_Posts">
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Votes" Type="XDbModel.Store.Votes" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Posts">
<PropertyRef Name="PostId" />
</Principal>
<Dependent Role="Votes">
<PropertyRef Name="PostId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="XDbModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="XDbEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Bundles" EntityType="XDbModel.Bundle" />
<EntitySet Name="CellProviders" EntityType="XDbModel.CellProvider" />
<EntitySet Name="Comments" EntityType="XDbModel.Comment" />
<EntitySet Name="LocationPoints" EntityType="XDbModel.LocationPoint" />
<EntitySet Name="Locations" EntityType="XDbModel.Location" />
<EntitySet Name="Offers" EntityType="XDbModel.Offer" />
<EntitySet Name="PostMedias" EntityType="XDbModel.PostMedia" />
<EntitySet Name="Posts" EntityType="XDbModel.Post" />
<EntitySet Name="ResourceEdges" EntityType="XDbModel.ResourceEdge" />
<EntitySet Name="ResourceNames" EntityType="XDbModel.ResourceName" />
<EntitySet Name="Resources" EntityType="XDbModel.Resource" />
<EntitySet Name="sysdiagrams" EntityType="XDbModel.sysdiagram" />
<EntitySet Name="Users" EntityType="XDbModel.User" />
<EntitySet Name="Votes" EntityType="XDbModel.Vote" />
<AssociationSet Name="FK_Comments_Offers" Association="XDbModel.FK_Comments_Offers">
<End Role="Offers" EntitySet="Offers" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_Comments_Users" Association="XDbModel.FK_Comments_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.FK_LocationPoints_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="LocationPoints" EntitySet="LocationPoints" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Locations" Association="XDbModel.FK_Posts_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Users_Locations" Association="XDbModel.FK_Users_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Users" EntitySet="Users" />
</AssociationSet>
<AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.FK_Posts_ResourceNames">
<End Role="ResourceNames" EntitySet="ResourceNames" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Users" Association="XDbModel.FK_Posts_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Votes_Posts" Association="XDbModel.FK_Votes_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="Votes" EntitySet="Votes" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.FK_ResourceEdges_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.FK_ResourceEdges_Resources1">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.FK_ResourceNames_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceNames" EntitySet="ResourceNames" />
</AssociationSet>
<AssociationSet Name="PostBundleJunction" Association="XDbModel.PostBundleJunction">
<End Role="Bundles" EntitySet="Bundles" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Bundle">
<Key>
<PropertyRef Name="BundleId" />
</Key>
<Property Name="BundleId" Type="Int32" Nullable="false" />
<NavigationProperty Name="Posts" Relationship="XDbModel.PostBundleJunction" FromRole="Bundles" ToRole="Posts" />
</EntityType>
<EntityType Name="CellProvider">
<Key>
<PropertyRef Name="CellProviderID" />
</Key>
<Property Name="CellProviderID" Type="Guid" Nullable="false" />
</EntityType>
<EntityType Name="Comment">
<Key>
<PropertyRef Name="CommentID" />
Ah, it's actually very simple, once you think about it..
What you're trying to do it add the resource to the data context, even though this resource object was received from the database in the first place. So the error is not complaining about your child objects, but about your parent object! :)
Basically if you comment the first line in the SaveResource() method it should just work fine!
E.g.:
public void SaveResource(Resource resource)
{
// context.AddToResources(resource); //Should also add children.
context.SaveChanges();
}
Or just move the AddChildResource method to your DAL:
public void AddChildResource(int parentResourceId, Resource childResource)
{
Resource parentResource = repository.GetResource(parentResourceId);
ResourceEdge inEdge = new ResourceEdge();
inEdge.ToResource = childResource;
parentResource.ToEdges.Add(inEdge);
context.SaveChanges();
}
Presumably it's complaining about the childResource not being in the added state. You added it to the edge object that is in the added state.
Can you try flipping the way you set that association round:-
public void AddChildResource(int parentResourceId, Resource childResource)
{
Resource parentResource = repository.GetResource(parentResourceId);
ResourceEdge inEdge = new ResourceEdge();
// inEdge.ToResource = childResource; <-- don't do this
parentResource.ToEdges.Add(inEdge);
childResource.FromEdges.Add(inEdge); // try this instead
repository.SaveResource(parentResource);
}

Resources