This question is locked. New answers and comments are not allowed.
I will start directly with the thing I am trying to do - the entities are the following. Document has a collection DocumentDetails and each DocumentDetail has a collection of DocumentDetailProperty entities. As I want trackable collections the code looks like this:
// Document entity
private TrackedBindingList<DocumentDetail> _documentDetails = new TrackedBindingList<DocumentDetail>();
public virtual TrackedBindingList<DocumentDetail> DocumentDetails
{
get
{
return this._documentDetails;
}
}
// DocumentDetail entity
private TrackedBindingList<DocumentDetailProperty> _documentDetailProperties = new TrackedBindingList<DocumentDetailProperty>();
public virtual TrackedBindingList<DocumentDetailProperty> DocumentDetailProperties
{
get
{
return this._documentDetailProperties;
}
} My scenario is the following: user creates a new DocumentDetail, adds some DocumentDetailProperty entities and saves all changes. The code for both the additions is:
// adding a DocumentDetail
DocumentDetail dd = new DocumentDetail() { DocumentId = theDoc.DocumentId };
theDoc.DocumentDetails.Add(dd);
context.Add(dd); // is this necessary?
// adding a DocumentDetailProperty
DocumentDetailProperty newDDP = new DocumentDetailProperty();
newDDP.DocumentDetailId = SelectedDocumentDetail.DocumentDetailId; // getting id from selected item. Should handle differently when selected item is just added? It makes no difference when this line is commented
SelectedDocumentDetail.DocumentDetailProperties.Add(newDDP);
context.Add(newDDP);
Save is performed by this code:
context.FlushChanges();
// just for testing - shold not matter in this scenario
context.ExecuteQuery<object>("uspAfterSaveTest", CommandType.StoredProcedure,
new OAParameter[] { new OAParameter("SomeString", "after save") });
context.SaveChanges();
the profiler giving the following:
declare @p1 int
set @p1=NULL
exec sp_prepexec @p1 output,N'@p0 int,@p1 int,@p2 varchar(128)',N'declare @generated_ids table([DocumentDetailPropertyId] int)
insert [DocumentDetailProperty] ([DocumentDetailId], [PropertyId], [PropertyValue])
output inserted.[DocumentDetailPropertyId] into @generated_ids
VALUES (@p0, @p1, @p2)
select t.[DocumentDetailPropertyId]
from @generated_ids as g join [DocumentDetailProperty] as t on g.[DocumentDetailPropertyId] = t.[DocumentDetailPropertyId]
where @@ROWCOUNT > 0',@p0=0,@p1=1,@p2='rewrwer'
select @p1
So, it tries to add only the DocumentDetailProperty entity and it fails as it tries to insert with a DocumentDetailId = 0. How can I perform both the additions with one save? Beside using TrackableCollections, I have mapping type = Xml (this was necessary in order to make TrackableCollection work, as indicated by Telerik in another thread).
Thank you,
Alex D.
// Document entity
private TrackedBindingList<DocumentDetail> _documentDetails = new TrackedBindingList<DocumentDetail>();
public virtual TrackedBindingList<DocumentDetail> DocumentDetails
{
get
{
return this._documentDetails;
}
}
// DocumentDetail entity
private TrackedBindingList<DocumentDetailProperty> _documentDetailProperties = new TrackedBindingList<DocumentDetailProperty>();
public virtual TrackedBindingList<DocumentDetailProperty> DocumentDetailProperties
{
get
{
return this._documentDetailProperties;
}
} My scenario is the following: user creates a new DocumentDetail, adds some DocumentDetailProperty entities and saves all changes. The code for both the additions is:
// adding a DocumentDetail
DocumentDetail dd = new DocumentDetail() { DocumentId = theDoc.DocumentId };
theDoc.DocumentDetails.Add(dd);
context.Add(dd); // is this necessary?
// adding a DocumentDetailProperty
DocumentDetailProperty newDDP = new DocumentDetailProperty();
newDDP.DocumentDetailId = SelectedDocumentDetail.DocumentDetailId; // getting id from selected item. Should handle differently when selected item is just added? It makes no difference when this line is commented
SelectedDocumentDetail.DocumentDetailProperties.Add(newDDP);
context.Add(newDDP);
Save is performed by this code:
context.FlushChanges();
// just for testing - shold not matter in this scenario
context.ExecuteQuery<object>("uspAfterSaveTest", CommandType.StoredProcedure,
new OAParameter[] { new OAParameter("SomeString", "after save") });
context.SaveChanges();
the profiler giving the following:
declare @p1 int
set @p1=NULL
exec sp_prepexec @p1 output,N'@p0 int,@p1 int,@p2 varchar(128)',N'declare @generated_ids table([DocumentDetailPropertyId] int)
insert [DocumentDetailProperty] ([DocumentDetailId], [PropertyId], [PropertyValue])
output inserted.[DocumentDetailPropertyId] into @generated_ids
VALUES (@p0, @p1, @p2)
select t.[DocumentDetailPropertyId]
from @generated_ids as g join [DocumentDetailProperty] as t on g.[DocumentDetailPropertyId] = t.[DocumentDetailPropertyId]
where @@ROWCOUNT > 0',@p0=0,@p1=1,@p2='rewrwer'
select @p1
So, it tries to add only the DocumentDetailProperty entity and it fails as it tries to insert with a DocumentDetailId = 0. How can I perform both the additions with one save? Beside using TrackableCollections, I have mapping type = Xml (this was necessary in order to make TrackableCollection work, as indicated by Telerik in another thread).
Thank you,
Alex D.