- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Saving related data recursively using adapter template
Joined: 20-Nov-2007
I am using adapter tempalte.
I have relation like Form Table->FormElemntTable->FormElemntAttributeTable. (-> : 1:many relation).
When I am saving the form entity I want related formelemntEntity and its related form elemntAttributeEntity should also get save.
How to achieve this.
Joined: 20-Nov-2007
Hi here is the code
IDataAccessAdapter adapter = this.formSO.GetAdapter();
EntityCollection<FormFormElementEntity> formFormElementEntityCollection = this.formSO.FormEntity.FormElements;
formFormElementEntityCollection.Clear();
int i = 0;
foreach (Element element in this.items)
{
formFormElementEntityCollection.AddNew();
formFormElementEntityCollection[i].ElementID = element.ElementID;
formFormElementEntityCollection[i].Name = element.Name;
formFormElementEntityCollection[i].Ordinal = element.Ordinal;
formFormElementEntityCollection[i].Required = element.Required;
EntityCollection<FormFormElementFormElementAttributeEntity> formFormElementFormElementAttributeEntityCollection = formFormElementEntityCollection[i].FormElements;
formFormElementFormElementAttributeEntityCollection.Clear();
int j = 0;
i++;
foreach (Attribute attribute in element.Atributes)
{
formFormElementFormElementAttributeEntityCollection.AddNew();
//formFormElementFormElementAttributeEntityCollection[j].FormFormElementID = formFormElementEntityCollection[i].ID;
formFormElementFormElementAttributeEntityCollection[j].FormElementAttributeID = attribute.FormElementAttributeID;
formFormElementFormElementAttributeEntityCollection[j].Value = attribute.StringValue;
formFormElementFormElementAttributeEntityCollection[j].MultiLingualValue = attribute.MultilingualValue;
j++;
}
}
return adapter.SaveEntity(this.formSO.FormEntity, true,true);
In the above code formSO is service object for formEnttiy
public FormSO(int formID, bool forDesigner)
: base()
{
FormEntity entity = new FormEntity(formID);
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FormEntity);
IPrefetchPathElement2 prefetchPathFormFormElement = prefetchPath.Add(FormEntity.PrefetchPathFormElements);
IPrefetchPathElement2 prefetchPathFormFormElementFormElementAttribute = prefetchPathFormFormElement.SubPath.Add(FormFormElementEntity.PrefetchPathFormElements);//to prefetch the related entities of type 'FormFormElementFormElementAttribute'
GetDataAdapter().FetchEntity(entity, prefetchPath);
}
Joined: 20-Nov-2007
_Exception:_An exception was caught during the execution of an action query: **Cannot insert the value NULL into column 'Form_FormElementID', **table 'cbase3development.dbo.Form_FormElement_FormElementAttribute'; column does not allow nulls. INSERT fails. The statement has been terminated.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.
Output:
- Method Enter: DataAccessAdapterBase.SaveEntity(4)
- Active Entity Description:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormEntity. ObjectID: 6807a9d2-d1a4-4ea9-ac81-d61239d62eef
- PrimaryKey field: ID. Type: System.Int32. Value: 12
- Method Enter: DataAccessAdapterBase.DetermineActionQueues(7)
- Active Entity Description:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormEntity. ObjectID: 6807a9d2-d1a4-4ea9-ac81-d61239d62eef
- PrimaryKey field: ID. Type: System.Int32. Value: 12
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementEntity. ObjectID: e61a71e1-e218-4bc5-97c5-3101a746d09d
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 6b246ec6-fb59-4d03-a952-51323212410e
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: c0082b99-f4c6-4215-b7bb-f15a09d419c8
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 240e3321-7bb0-4a95-bd58-a4d01598f8da
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 89554b04-9352-4802-9729-a6679fb8568a
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementEntity. ObjectID: 336f0465-7f86-422f-9fe9-95d92883e190
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 77e110b8-91f1-48fd-8d36-a78cbf6e4a7d
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 624543e4-77a3-453b-b8d7-ff766ae2682d
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: a6434a61-4115-48df-b209-c608cc34a4e0
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Entity added to insert queue:
- Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: a678b6be-36d9-4c2a-9e11-98c08a122bfc
- PrimaryKey field: ID. Type: System.Int32. Value: <undefined value>
- Method Exit: DataAccessAdapterBase.DetermineActionQueues(7)
- Method Enter: DataAccessAdapterBase.StartTransaction
- Transaction name: RecursiveSave. Isolation level: ReadCommitted. Method Enter: DataAccessAdapterBase.OpenConnection
- New connection created.
- Connection physically opened. Method Exit: DataAccessAdapterBase.OpenConnection Method Exit: DataAccessAdapterBase.StartTransaction Method Enter: DataAccessAdapterBase.PersistQueue Persistence action info: Action: Insert. Queue length: 10 Current persisted entity info: Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementEntity. ObjectID: e61a71e1-e218-4bc5-97c5-3101a746d09d PrimaryKey field: ID. Type: System.Int32. Value: <undefined value> Method Enter: CreateInsertDQ Method Enter: CreateSingleTargetInsertDQ Generated Sql query: Query: INSERT INTO [cbase3development].[dbo].[Form_FormElement] ([FormID], [ElementID], [Name], [Ordinal], [Required]) VALUES (@FormID, @ElementID, @Name, @Ordinal, @Required);SELECT @ID=SCOPE_IDENTITY() Parameter: @ID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>. Parameter: @FormID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12. Parameter: @ElementID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Name : Xml. Length: 2147483647. Precision: 0. Scale: 0. Direction: Input. Value: <?xml version='1.0'?><localizedstring xmlns="http://www.orangehill.nl/schema/cb3/localizedstring"><value locale="en-US">hjghj</value></localizedstring>. Parameter: @Ordinal : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Required : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: False.
- Method Exit: CreateSingleTargetInsertDQ
- Method Exit: CreateInsertDQ
- Method Enter: DataAccessAdapterBase.ExecuteActionQuery
- Method Enter: DataAccessAdapterBase.OpenConnection
- Method Exit: DataAccessAdapterBase.OpenConnection
- Method Enter: Query.ReflectOutputValuesInRelatedFields
- Syncing field ID with parameter @ID. Method Exit: Query.ReflectOutputValuesInRelatedFields Executed Sql Query: Query: INSERT INTO [cbase3development].[dbo].[Form_FormElement] ([FormID], [ElementID], [Name], [Ordinal], [Required]) VALUES (@FormID, @ElementID, @Name, @Ordinal, @Required);SELECT @ID=SCOPE_IDENTITY() Parameter: @ID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: 73. Parameter: @FormID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 12. Parameter: @ElementID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Name : Xml. Length: 2147483647. Precision: 0. Scale: 0. Direction: Input. Value: <?xml version='1.0'?><localizedstring xmlns="http://www.orangehill.nl/schema/cb3/localizedstring"><value locale="en-US">hjghj</value></localizedstring>. Parameter: @Ordinal : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Required : Boolean. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: False.
Method Exit: DataAccessAdapterBase.ExecuteActionQuery Method Enter: DataAccessAdapterBase.FetchEntity(1) Active Entity Description: Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementEntity. ObjectID: e61a71e1-e218-4bc5-97c5-3101a746d09d PrimaryKey field: ID. Type: System.Int32. Value: 73 Method Enter: DataAccessAdapterBase.FetchEntityUsingFilter(3) Method Enter: CreatePagingSelectDQ Method Enter: CreateSelectDQ Method Enter: CreateSelectDQ Generated Sql query: Query: SELECT [cbase3development].[dbo].[Form_FormElement].[ID], [cbase3development].[dbo].[Form_FormElement].[FormID], [cbase3development].[dbo].[Form_FormElement].[ElementID], [cbase3development].[dbo].[Form_FormElement].[Name], [cbase3development].[dbo].[Form_FormElement].[Ordinal], [cbase3development].[dbo].[Form_FormElement].[Required], [cbase3development].[dbo].[Form_FormElement].[CreationDate], [cbase3development].[dbo].[Form_FormElement].[LastChangeDate] FROM [cbase3development].[dbo].[Form_FormElement] WHERE ( ( [cbase3development].[dbo].[Form_FormElement].[ID] = @ID1)) Parameter: @ID1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 73. Method Exit: CreateSelectDQ Method Exit: CreatePagingSelectDQ: no paging. Method Enter: DataAccessAdapterBase.ExecuteSingleRowRetrievalQuery Method Enter: DataAccessAdapterBase.OpenConnection Method Exit: DataAccessAdapterBase.OpenConnection Executed Sql Query: Query: SELECT [cbase3development].[dbo].[Form_FormElement].[ID], [cbase3development].[dbo].[Form_FormElement].[FormID], [cbase3development].[dbo].[Form_FormElement].[ElementID], [cbase3development].[dbo].[Form_FormElement].[Name], [cbase3development].[dbo].[Form_FormElement].[Ordinal], [cbase3development].[dbo].[Form_FormElement].[Required], [cbase3development].[dbo].[Form_FormElement].[CreationDate], [cbase3development].[dbo].[Form_FormElement].[LastChangeDate] FROM [cbase3development].[dbo].[Form_FormElement] WHERE ( ( [cbase3development].[dbo].[Form_FormElement].[ID] = @ID1)) Parameter: @ID1 : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 73. Method Exit: DataAccessAdapterBase.ExecuteSingleRowRetrievalQuery Method Exit: DataAccessAdapterBase.FetchEntityUsingFilter Method Exit: DataAccessAdapterBase.FetchEntity(1) Current persisted entity info: Entity: Orangehill.CBase3.LLBL.EntityClasses.FormFormElementFormElementAttributeEntity. ObjectID: 6b246ec6-fb59-4d03-a952-51323212410e PrimaryKey field: ID. Type: System.Int32. Value: <undefined value> Method Enter: CreateInsertDQ Method Enter: CreateSingleTargetInsertDQ Generated Sql query: Query: INSERT INTO [cbase3development].[dbo].[Form_FormElement_FormElementAttribute] ([FormElementAttributeID], [Value], [MultiLingualValue]) VALUES (@FormElementAttributeID, @Value, @MultiLingualValue);SELECT @ID=SCOPE_IDENTITY() _Parameter: @ID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Output. Value: <undefined value>._Parameter: @FormElementAttributeID : Int32. Length: 0. Precision: 10. Scale: 0. Direction: Input. Value: 1. Parameter: @Value : String. Length: 2147483647. Precision: 0. Scale: 0. Direction: Input. Value: "1000". Parameter: @MultiLingualValue : Xml. Length: 2147483647. Precision: 0. Scale: 0. Direction: Input. Value: <undefined value>.
Method Exit: CreateSingleTargetInsertDQ Method Exit: CreateInsertDQ Method Enter: DataAccessAdapterBase.ExecuteActionQuery Method Enter: DataAccessAdapterBase.OpenConnection Method Exit: DataAccessAdapterBase.OpenConnection Method Exit: DataAccessAdapterBase.ExecuteActionQuery Method Enter: DataAccessAdapterBase.Rollback Method Enter: DataAccessAdapterBase.Reset Method Exit: DataAccessAdapterBase.Reset Method Exit: DataAccessAdapterBase.Rollback A first chance exception of type 'SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException' occurred in SD.LLBLGen.Pro.ORMSupportClasses.NET20.DLL Method Enter: DataAccessAdapterBase.CloseConnection Method Exit: DataAccessAdapterBase.CloseConnection Method Exit: DataAccessAdapterBase.SaveEntity(4)
Don't use Collection.AddNew() method.
Reference wrote:
Do not call this method from your own code. This is a databinding ONLY method.
I think it waits for the EndEdit() call, to end the editing cycle.
Instead Create a new entity and then Add it to the Collection using Add() method.