Can not directly save a child object (CSLA 4.5)

Can not directly save a child object (CSLA 4.5)

Old forum URL: forums.lhotka.net/forums/t/12841.aspx


issuarez posted on Saturday, March 07, 2015

I am baffled as to why I am getting this error given that I am calling save on the Collection not the child. Any thoughts?

 

Class Structure:

CountryList – EditableRootCollection

Country – EditableChild

 

Code: 

Country mCountry = Country.NewCountry();

mCountry.CountryCode = "TST";

mCountry.CountryName = "Test Country";

 

mCountryList = CountryList.GetAll();

mCountryList.Add(mCountry);

mCountryList =  mCountryList.Save();

 

Exception:

DataPortal.Update failed (Can not directly save a child object)

ajj3085 replied on Sunday, March 08, 2015

You're probably subclassing EditableRootListBase for your collection. That class should be used only in the specific scenario where you show the list in a grid and want each row saved when you move to the next root. That is, it's meant to contain root objects not children. Subclass BusinessListBase or BusinessBindingListBase if you're using WinForms instead.

JonnyBee replied on Sunday, March 08, 2015

Hi,

You have the sequence all wrong. 

Country.NewCountry returns a new editable root object.You may call save on this root object all by itself.

CountryList.GetAll returns a list of editable child objects.

If you want to use the list then

mCountryList = CountryList.GetAll();

Country mCountry = list.AddNew();   // will return a new child object

mCountry.CountryCode = "TST";
mCountry.CountryName = "Test Country";
mCountryList =  mCountryList.Save();

Or just call Save on the mCountry.

There is no need to add a new object to the list before you call Save.

And when you do have the list call AddNew() (IIRC) to get a new item.

issuarez replied on Sunday, March 08, 2015

CountryList is subclassed from BusinessListBase. Country is subclased from BusinessBase.

You are right in that I am trying to work from a DevExpress grid I have binded to the collection. I am hoping to let the user make their changes and persist them once they press a Save button. I keep getting the exception every time I try to Save CountryList though. I tried with a few different classes too, same issue. I just started having this problem with the new version of CSLA, CodeSmith and DevExpress - not sure which one might be causing the issue...

ajj3085 replied on Sunday, March 08, 2015

In your factory method for Country are you calling DataPortal.CreateChild<Counrty> ?

Can you post the full code for both classes?

issuarez replied on Sunday, March 08, 2015

I don't here a way to add an attachment here so here goes CountryList:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//------------------------------------------------------------------------------

// <autogenerated>

// This code was generated using CodeSmith: v7.0.2, CSLA Templates: v4.0.0.0, CSLA Framework: v4.5.x.

// Changes to this file will be lost after each regeneration.

// To extend the functionality of this class, please modify the partial class 'CountryList.cs'.

//

// Template: EditableRootList.Generated.cst

// Template website: http://code.google.com/p/codesmith/

// </autogenerated>

//------------------------------------------------------------------------------

using

 

 

 

System;

using

 

 

 

System.Linq;

using

 

 

 

System.Threading.Tasks;

using

 

 

 

Csla;

using

 

 

 

Csla.Data;

namespace

 

 

 

QFERPCommon.Business

{

[

 

 

Serializable

]

 

 

 

public partial class CountryList : BusinessListBase<CountryList, Country

>

{

#region

 

 

 

Contructor(s)

 

 

 

public

CountryList()

{

AllowNew =

 

 

true

;

}

#endregion

#region

 

 

 

Method Overrides

 

 

 

protected override Country

AddNewCore()

{

 

 

 

Country item = QFERPCommon.Business.Country

.NewCountry();

 

 

 

bool cancel = false

;

OnAddNewCore(

 

 

ref item, ref

cancel);

 

 

 

if

(!cancel)

{

 

 

 

// Check to see if someone set the item to null in the OnAddNewCore.

 

 

 

if(item == null

)

item = QFERPCommon.Business.

 

 

Country

.NewCountry();

 

Add(item);

}

 

 

 

return

item;

}

 

#endregion

#region

 

 

 

Synchronous Factory Methods

 

 

 

///

<summary>

 

 

 

/// Creates a new object of type <see cref="CountryList"/>

.

 

 

 

///

</summary>

 

 

 

/// <returns>Returns a newly instantiated collection of type <see cref="CountryList"/>.

</returns>

 

 

 

public static CountryList

NewList()

{

 

 

 

return DataPortal.Create<CountryList

>();

}

 

 

 

///

<summary>

 

 

 

/// Returns a <see cref="CountryList"/>

object of the specified criteria.

 

 

 

///

</summary>

 

 

 

/// <param name="countryId">No additional detail available.

</param>

 

 

 

/// <returns>A <see cref="CountryList"/> object of the specified criteria.

</returns>

 

 

 

public static CountryList GetByCountryId(System.Int32

countryId)

{

 

 

 

var criteria = new CountryCriteria

{CountryId = countryId};

 

 

 

 

 

return DataPortal.Fetch<CountryList

>(criteria);

}

 

 

 

public static CountryList GetByCriteria(CountryCriteria

criteria)

{

 

 

 

return DataPortal.Fetch<CountryList

>(criteria);

}

 

 

 

 

public static CountryList

GetAll()

{

 

 

 

return DataPortal.Fetch<CountryList>(new CountryCriteria

());

}

#endregion

#region

 

 

 

Asynchronous Factory Methods

 

 

 

public static async Task<CountryList

> NewListAsync()

{

 

 

 

return await DataPortal.CreateAsync<CountryList

>();

}

 

 

 

 

public static async Task<CountryList> GetByCountryIdAsync(System.Int32

countryId)

{

 

 

 

var criteria = new CountryCriteria

{CountryId = countryId};

 

 

 

 

 

return await DataPortal.FetchAsync<CountryList

>(criteria);

}

 

 

 

public static async Task<CountryList> GetByCriteriaAsync(CountryCriteria

criteria)

{

 

 

 

return await DataPortal.FetchAsync<CountryList

>(criteria);

}

 

 

 

public static async Task<CountryList

> GetAllAsync()

{

 

 

 

return await DataPortal.FetchAsync<CountryList>(new CountryCriteria

());

}

#endregion

#region

 

 

 

DataPortal partial methods

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when creating the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object creation should proceed.

</param>

 

 

 

partial void OnCreating(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been created.

 

 

 

///

</summary>

 

 

 

partial void

OnCreated();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when fetching the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria"><see cref="CountryCriteria"/> object containing the criteria of the object to fetch.

</param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object fetching should proceed.

</param>

 

 

 

partial void OnFetching(CountryCriteria criteria, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been fetched.

 

 

 

///

</summary>

 

 

 

partial void

OnFetched();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when mapping the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object mapping should proceed.

</param>

 

 

 

partial void OnMapping(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when mapping the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

///

<param name="reader"></param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object mapping should proceed.

</param>

 

 

 

partial void OnMapping(SafeDataReader reader, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been mapped.

 

 

 

///

</summary>

 

 

 

partial void

OnMapped();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when updating the <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object creation should proceed.

</param>

 

 

 

partial void OnUpdating(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the <see cref="Country"/>

object has been updated.

 

 

 

///

</summary>

 

 

 

partial void

OnUpdated();

 

 

 

partial void OnAddNewCore(ref Country item, ref bool

cancel);

#endregion

#region

 

 

 

Exists Command

 

 

 

///

<summary>

 

 

 

///

Determines if a record exists in the Country in the database for the specified criteria.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria">The criteria parameter is a <see cref="CountryList"/> object.

</param>

 

 

 

/// <returns>A boolean value of true is returned if a record is found.

</returns>

 

 

 

public static bool Exists(CountryCriteria

criteria)

{

 

 

 

return QFERPCommon.Business.Country

.Exists(criteria);

}

 

 

 

///

<summary>

 

 

 

///

Determines if a record exists in the Country in the database for the specified criteria.

 

 

 

///

</summary>

 

 

 

public static async Task<bool> ExistsAsync(CountryCriteria

criteria)

{

 

 

 

return await QFERPCommon.Business.ExistsCommand

.ExecuteAsync(criteria);

}

#endregion

 

#region

 

 

 

Enhancements

 

 

 

public Country GetCountry(System.Int32

countryId)

{

 

 

 

return this

.FirstOrDefault(i => i.CountryId == countryId);

}

 

 

 

public bool Contains(System.Int32

countryId)

{

 

 

 

return this

.Count(i => i.CountryId == countryId) > 0;

}

 

 

 

 

public bool ContainsDeleted(System.Int32

countryId)

{

 

 

 

return

DeletedList.Count(i => i.CountryId == countryId) > 0;

}

 

 

 

 

public void Remove(System.Int32

countryId)

{

 

 

 

var item = this

.FirstOrDefault(i => i.CountryId == countryId);

 

 

 

if (item != null

)

Remove(item);

}

#endregion

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//------------------------------------------------------------------------------

// <autogenerated>

// This code was generated using CodeSmith: v7.0.2, CSLA Templates: v4.0.0.0, CSLA Framework: v4.5.x.

// Changes to this file will be lost after each regeneration.

// To extend the functionality of this class, please modify the partial class 'CountryList.cs'.

//

// Template: EditableRootList.DataAccess.ParameterizedSQL.cst

// Template website: http://code.google.com/p/codesmith/

// </autogenerated>

//------------------------------------------------------------------------------

using

 

 

 

System;

using

 

 

 

System.Data;

using

 

 

 

System.Data.SqlClient;

using

 

 

 

Csla;

using

 

 

 

Csla.Data;

namespace

 

 

 

QFERPCommon.Business

{

 

 

 

public partial class

CountryList

{

[

 

 

RunLocal

]

 

 

 

protected override void

DataPortal_Create()

{

}

 

 

 

private void DataPortal_Fetch(CountryCriteria

criteria)

{

 

 

 

bool cancel = false

;

OnFetching(criteria,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

RaiseListChangedEvents =

 

 

false

;

 

 

 

// Fetch Child objects.

 

 

 

string commandText = String.Format("SELECT [CountryId], [CountryCode], [CountryName] FROM [dbo].[Country] {0}", ADOHelper

.BuildWhereStatement(criteria.StateBag));

 

 

 

using (var connection = new SqlConnection(ADOHelper

.ConnectionString))

{

connection.Open();

 

 

 

using (var command = new SqlCommand

(commandText, connection))

{

command.Parameters.AddRange(

 

 

ADOHelper

.SqlParameters(criteria.StateBag));

 

 

 

using(var reader = new SafeDataReader

(command.ExecuteReader()))

{

 

 

 

if

(reader.Read())

{

 

 

 

do

{

 

 

 

this.Add(QFERPCommon.Business.Country

.GetCountry(reader));

}

 

 

while

(reader.Read());

}

}

}

}

RaiseListChangedEvents =

 

 

true

;

OnFetched();

}

[

 

 

Transactional(TransactionalTypes

.TransactionScope)]

 

 

 

protected override void

DataPortal_Update()

{

 

 

 

bool cancel = false

;

OnUpdating(

 

 

ref

cancel);

 

 

 

if (cancel) return

;

RaiseListChangedEvents =

 

 

false

;

 

 

 

for (int

index = 0; index < DeletedList.Count; index++)

{

DeletedList[index] = DeletedList[index].Save();

}

 

DeletedList.Clear();

 

 

 

for (int

index = 0; index < Items.Count; index++)

{

Items[index] = Items[index].Save();

 

}

RaiseListChangedEvents =

 

 

true

;

 

 

 

JonnyBee replied on Monday, March 09, 2015

Hi,

There is a button for Attachments on the "Options" tab when you create post/answer.

In cases where you want to post code snippets please install "Productivity Power Tools" in Visual Studio.  
PPT will provide "HTML Copy" so that you get the code as HTML and looks much nicer.  

I suggest you ask this question on the CodeSmith CSLA Template forum. 

The default implementation in CSLA for <list>.AddNew() will return a newly created child object. 

issuarez replied on Sunday, March 08, 2015

Country:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//------------------------------------------------------------------------------

// <autogenerated>

// This code was generated using CodeSmith: v7.0.2, CSLA Templates: v4.0.0.0, CSLA Framework: v4.5.x.

// Changes to this file will be lost after each regeneration.

// To extend the functionality of this class, please modify the partial class 'Country.cs'.

//

// Template: EditableChild.Generated.cst

// Template website: http://code.google.com/p/codesmith/

// </autogenerated>

//------------------------------------------------------------------------------

using

 

 

 

System;

using

 

 

 

System.Threading.Tasks;

using

 

 

 

Csla;

using

 

 

 

Csla.Data;

using

 

 

 

System.Data.SqlClient;

using

 

 

 

Csla.Rules;

namespace

 

 

 

QFERPCommon.Business

{

[

 

 

Serializable

]

 

 

 

public partial class Country : BusinessBase<Country

>

{

#region

 

 

 

Contructor(s)

 

 

 

public

Country()

{

 

 

/* Require use of factory methods */

}

#endregion

 

#region

 

 

 

Business Rules

 

 

 

///

<summary>

 

 

 

///

Contains the CodeSmith generated validation rules.

 

 

 

///

</summary>

 

 

 

protected override void

AddBusinessRules()

{

 

 

 

// Call the base class, if this call isn't made than any declared System.ComponentModel.DataAnnotations rules will not work.

 

 

 

base

.AddBusinessRules();

 

 

 

if

(AddBusinessValidationRules())

 

 

 

return

;

BusinessRules.AddRule(

 

 

new Csla.Rules.CommonRules.Required

(_countryCodeProperty));

BusinessRules.AddRule(

 

 

new Csla.Rules.CommonRules.MaxLength

(_countryCodeProperty, 3));

BusinessRules.AddRule(

 

 

new Csla.Rules.CommonRules.Required

(_countryNameProperty));

BusinessRules.AddRule(

 

 

new Csla.Rules.CommonRules.MaxLength

(_countryNameProperty, 50));

}

#endregion

#region

 

 

 

Properties

 

 

 

private static readonly PropertyInfo<System.Int32> _countryIdProperty = RegisterProperty<System.Int32>(p => p.CountryId, "Country Id"

);

[System.ComponentModel.

 

 

DataObjectField(true, true

)]

 

 

 

public System.Int32

CountryId

{

 

 

 

get { return

GetProperty(_countryIdProperty); }

 

 

 

internal set{ SetProperty(_countryIdProperty, value

); }

}

 

 

 

private static readonly PropertyInfo<System.String> _countryCodeProperty = RegisterProperty<System.String>(p => p.CountryCode, "Country Code"

);

 

 

 

public System.String

CountryCode

{

 

 

 

get { return

GetProperty(_countryCodeProperty); }

 

 

 

set{ SetProperty(_countryCodeProperty, value

); }

}

 

 

 

private static readonly PropertyInfo<System.String> _countryNameProperty = RegisterProperty<System.String>(p => p.CountryName, "Country Name"

);

 

 

 

public System.String

CountryName

{

 

 

 

get { return

GetProperty(_countryNameProperty); }

 

 

 

set{ SetProperty(_countryNameProperty, value

); }

}

 

 

 

// OneToMany

 

 

 

private static readonly PropertyInfo<CityList> _citiesProperty = RegisterProperty<CityList>(p => p.Cities, Csla.RelationshipTypes

.Child);

 

 

 

public CityList

Cities

{

 

 

 

get

{

 

 

 

bool cancel = false

;

OnChildLoading(_citiesProperty,

 

 

ref

cancel);

 

 

 

 

if

(!cancel)

{

 

 

 

if

(!FieldManager.FieldExists(_citiesProperty))

{

 

 

 

var criteria = new QFERPCommon.Business.CityCriteria

{CountryId = CountryId};

 

 

 

 

 

if(!QFERPCommon.Business.CityList

.Exists(criteria))

LoadProperty(_citiesProperty, QFERPCommon.Business.

 

 

CityList

.NewList());

 

 

 

else

LoadProperty(_citiesProperty, QFERPCommon.Business.

 

 

CityList

.GetByCountryId(CountryId));

}

}

 

 

 

return

GetProperty(_citiesProperty);

}

}

 

#endregion

#region

 

 

 

Synchronous Factory Methods

 

 

 

///

<summary>

 

 

 

/// Creates a new object of type <see cref="Country"/>

.

 

 

 

///

</summary>

 

 

 

/// <returns>Returns a newly instantiated collection of type <see cref="Country"/>.

</returns>

 

 

 

internal static Country

NewCountry()

{

 

 

 

return DataPortal.CreateChild<Country

>();

}

 

 

 

internal static Country GetCountry(SafeDataReader

reader)

{

 

 

 

return DataPortal.FetchChild<Country

>(reader);

}

 

 

 

///

<summary>

 

 

 

/// Returns a <see cref="Country"/>

object of the specified criteria.

 

 

 

///

</summary>

 

 

 

/// <param name="countryId">No additional detail available.

</param>

 

 

 

/// <returns>A <see cref="Country"/> object of the specified criteria.

</returns>

 

 

 

internal static Country GetByCountryId(System.Int32

countryId)

{

 

 

 

var criteria = new CountryCriteria

{CountryId = countryId};

 

 

 

 

 

return DataPortal.FetchChild<Country

>(criteria);

}

#endregion

#region

 

 

 

Asynchronous Factory Methods

 

 

 

internal static async Task<Country

> NewCountryAsync()

{

 

 

 

return await DataPortal.CreateAsync<Country

>();

}

 

 

 

internal static async Task<Country> GetByCountryIdAsync(System.Int32

countryId)

{

 

 

 

var criteria = new CountryCriteria

{ CountryId = countryId};

 

 

 

 

 

return await DataPortal.FetchAsync<AsyncChildLoader<Country

>>(criteria).ContinueWith(t => t.Result.Child);

}

#endregion

 

#region

 

 

 

ChildPortal partial methods

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when creating the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object creation should proceed.

</param>

 

 

 

partial void OnChildCreating(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been created.

 

 

 

///

</summary>

 

 

 

partial void

OnChildCreated();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when fetching the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria"><see cref="CountryCriteria"/> object containing the criteria of the object to fetch.

</param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object fetching should proceed.

</param>

 

 

 

partial void OnChildFetching(CountryCriteria criteria, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been fetched.

 

 

 

///

</summary>

 

 

 

partial void

OnChildFetched();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when mapping the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object mapping should proceed.

</param>

 

 

 

partial void OnMapping(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when mapping the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

///

<param name="reader"></param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object mapping should proceed.

</param>

 

 

 

partial void OnMapping(SafeDataReader reader, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been mapped.

 

 

 

///

</summary>

 

 

 

partial void

OnMapped();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when inserting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object insertion should proceed.

</param>

 

 

 

partial void OnChildInserting(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when inserting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

///

<param name="connection"></param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object insertion should proceed.

</param>

 

 

 

partial void OnChildInserting(SqlConnection connection, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been inserted.

 

 

 

///

</summary>

 

 

 

partial void

OnChildInserted();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when updating the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object creation should proceed.

</param>

 

 

 

partial void OnChildUpdating(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when updating the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

partial void OnChildUpdating(SqlConnection connection, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been updated.

 

 

 

///

</summary>

 

 

 

partial void

OnChildUpdated();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when self deleting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object self deletion should proceed.

</param>

 

 

 

partial void OnChildSelfDeleting(ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when self deleting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

///

<param name="connection"></param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object self deletion should proceed.

</param>

 

 

 

partial void OnChildSelfDeleting(SqlConnection connection, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object has been deleted.

 

 

 

///

</summary>

 

 

 

partial void

OnChildSelfDeleted();

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when deleting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria"><see cref="CountryCriteria"/> object containing the criteria of the object to delete.

</param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object deletion should proceed.

</param>

 

 

 

partial void OnDeleting(CountryCriteria criteria, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called when deleting the child <see cref="Country"/>

object.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria"><see cref="CountryCriteria"/> object containing the criteria of the object to delete.

</param>

 

 

 

///

<param name="connection"></param>

 

 

 

/// <param name="cancel">Value returned from the method indicating whether the object deletion should proceed.

</param>

 

 

 

partial void OnDeleting(CountryCriteria criteria, SqlConnection connection, ref bool

cancel);

 

 

 

///

<summary>

 

 

 

/// CodeSmith generated stub method that is called after the child <see cref="Country"/>

object with the specified criteria has been deleted.

 

 

 

///

</summary>

 

 

 

partial void

OnDeleted();

 

 

 

partial void OnChildLoading(Csla.Core.IPropertyInfo childProperty, ref bool

cancel);

#endregion

#region

 

 

 

Exists Command

 

 

 

///

<summary>

 

 

 

///

Determines if a record exists in the Country table in the database for the specified criteria.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria">The criteria parameter is an <see cref="Country"/> object.

</param>

 

 

 

/// <returns>A boolean value of true is returned if a record is found.

</returns>

 

 

 

public static bool Exists(CountryCriteria

criteria)

{

 

 

 

return QFERPCommon.Business.ExistsCommand

.Execute(criteria);

}

 

 

 

///

<summary>

 

 

 

///

Determines if a record exists in the Country table in the database for the specifi

ed criteria.

 

 

 

///

</summary>

 

 

 

public static async Task<bool> ExistsAsync(CountryCriteria

criteria)

{

 

 

 

return await QFERPCommon.Business.ExistsCommand

.ExecuteAsync(criteria);

}

#endregion

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

//------------------------------------------------------------------------------

// <autogenerated>

// This code was generated using CodeSmith: v7.0.2, CSLA Templates: v4.0.0.0, CSLA Framework: v4.5.x.

// Changes to this file will be lost after each regeneration.

// To extend the functionality of this class, please modify the partial class 'Country.cs'.

//

// Template: EditableChild.DataAccess.ParameterizedSQL.cst

// Template website: http://code.google.com/p/codesmith/

// </autogenerated>

//------------------------------------------------------------------------------

using

 

 

 

System;

using

 

 

 

System.Data;

using

 

 

 

System.Data.SqlClient;

using

 

 

 

Csla;

using

 

 

 

Csla.Data;

namespace

 

 

 

QFERPCommon.Business

{

 

 

 

public partial class

Country

{

 

 

 

///

<summary>

 

 

 

/// Instantiates a CSLA editable child business object of type <see cref="Country"/>

.

 

 

 

///

</summary>

[

 

 

RunLocal

()]

 

 

 

protected override void

Child_Create()

{

 

 

 

bool cancel = false

;

OnChildCreating(

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

BusinessRules.CheckRules();

OnChildCreated();

}

 

 

 

///

<summary>

 

 

 

/// Retrieves data from the data base into a CSLA editable child business object of type <see cref="Country"/>

 

 

 

///

using the criteria provided.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria">Object of type

<see cref="CountryCriteria"/></param>

 

 

 

///

<returns></returns>

 

 

 

private void Child_Fetch(CountryCriteria

criteria)

{

 

 

 

bool cancel = false

;

OnChildFetching(criteria,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

 

 

string commandText = String.Format("SELECT [CountryId], [CountryCode], [CountryName] FROM [dbo].[Country] {0}", ADOHelper

.BuildWhereStatement(criteria.StateBag));

 

 

 

using (var connection = new SqlConnection(ADOHelper

.ConnectionString))

{

connection.Open();

 

 

 

using (var command = new SqlCommand

(commandText, connection))

{

command.Parameters.AddRange(

 

 

ADOHelper

.SqlParameters(criteria.StateBag));

 

 

 

using(var reader = new SafeDataReader

(command.ExecuteReader()))

{

 

 

 

if

(reader.Read())

Map(reader);

 

 

 

else

 

 

 

throw new System.Exception(String.Format("The record was not found in 'dbo.Country' using the following criteria: {0}."

, criteria));

}

}

}

OnChildFetched();

}

#region

 

 

 

Child_Insert

 

 

 

///

<summary>

 

 

 

///

Inserts data into the data base using the information in the current

 

 

 

/// CSLA editable child business object of type <see cref="Country"/>

 

 

 

///

</summary>

 

 

 

///

<returns></returns>

 

 

 

private void Child_Insert(SqlConnection

connection)

{

 

 

 

bool cancel = false

;

OnChildInserting(connection,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

 

 

if(connection.State != ConnectionState

.Open) connection.Open();

 

 

 

const string commandText = "INSERT INTO [dbo].[Country] ([CountryCode], [CountryName]) VALUES (@pCountryCode, @pCountryName); SELECT [CountryId] FROM [dbo].[Country] WHERE CountryId = SCOPE_IDENTITY()"

;

 

 

 

using(var command = new SqlCommand

(commandText, connection))

{

command.Parameters.AddWithValue(

 

 

"@pCountryCode", this

.CountryCode);

command.Parameters.AddWithValue(

 

 

"@pCountryName", this

.CountryName);

 

 

 

using(var reader = new SafeDataReader

(command.ExecuteReader()))

{

 

 

 

if

(reader.Read())

{

 

 

 

// Update identity primary key value.

LoadProperty(_countryIdProperty, reader.GetInt32(

 

 

"CountryId"

));

}

}

}

FieldManager.UpdateChildren(

 

 

this

, connection);

OnChildInserted();

}

#endregion

#region

 

 

 

Child_Update

 

 

 

///

<summary>

 

 

 

///

Updates the corresponding record in the data base with the information in the current

 

 

 

/// CSLA editable child business object of type <see cref="Country"/>

 

 

 

///

</summary>

 

 

 

///

<returns></returns>

 

 

 

private void Child_Update(SqlConnection

connection)

{

 

 

 

bool cancel = false

;

OnChildUpdating(connection,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

 

 

if(connection.State != ConnectionState

.Open) connection.Open();

 

 

 

const string commandText = "UPDATE [dbo].[Country] SET [CountryCode] = @pCountryCode, [CountryName] = @pCountryName WHERE [CountryId] = @pCountryId"

;

 

 

 

using(var command = new SqlCommand

(commandText, connection))

{

command.Parameters.AddWithValue(

 

 

"@pCountryId", this

.CountryId);

command.Parameters.AddWithValue(

 

 

"@pCountryCode", this

.CountryCode);

command.Parameters.AddWithValue(

 

 

"@pCountryName", this

.CountryName);

 

 

 

using(var reader = new SafeDataReader

(command.ExecuteReader()))

{

 

 

 

if

(reader.Read())

{

}

}

}

FieldManager.UpdateChildren(

 

 

this

, connection);

OnChildUpdated();

}

#endregion

 

 

 

///

<summary>

 

 

 

///

Deletes the corresponding record in the data base with the information in the current

 

 

 

/// CSLA editable child business object of type <see cref="Country"/>

 

 

 

///

</summary>

 

 

 

///

<returns></returns>

 

 

 

private void Child_DeleteSelf(SqlConnection

connection)

{

 

 

 

bool cancel = false

;

OnChildSelfDeleting(connection,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

DataPortal_Delete(

 

 

new CountryCriteria

(CountryId), connection);

 

OnChildSelfDeleted();

}

 

 

 

///

<summary>

 

 

 

///

Deletes data in the data base using the criteria specified in the CountryCriteria object.

 

 

 

///

</summary>

 

 

 

/// <param name="criteria">Object of type

<see cref="CountryCriteria"/></param>

 

 

 

///

<returns></returns>

 

 

 

protected void DataPortal_Delete(CountryCriteria criteria, SqlConnection

connection)

{

 

 

 

bool cancel = false

;

OnDeleting(criteria, connection,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

 

 

var commandText = String.Format("DELETE FROM [dbo].[Country] {0}", ADOHelper

.BuildWhereStatement(criteria.StateBag));

 

 

 

using (var command = new SqlCommand

(commandText, connection))

{

command.Parameters.AddRange(

 

 

ADOHelper

.SqlParameters(criteria.StateBag));

 

 

 

//result: The number of rows changed, inserted, or deleted. -1 for select statements; 0 if no rows were affected, or the statement failed.

 

 

 

int

result = command.ExecuteNonQuery();

 

 

 

if

(result == 0)

 

 

 

throw new DBConcurrencyException("The entity is out of date on the client. Please update the entity and try again. This could also be thrown if the sql statement failed to execute."

);

}

OnDeleted();

}

#region

 

 

 

Map

 

 

 

private void Map(SafeDataReader

reader)

{

 

 

 

bool cancel = false

;

OnMapping(reader,

 

 

ref

cancel);

 

 

 

if (cancel) return

;

 

 

 

using

(BypassPropertyChecks)

{

LoadProperty(_countryIdProperty, reader[

 

 

"CountryId"

]);

LoadProperty(_countryCodeProperty, reader[

 

 

"CountryCode"

]);

LoadProperty(_countryNameProperty, reader[

 

 

"CountryName"

]);

}

OnMapped();

}

 

 

 

 

private void Child_Fetch(SafeDataReader

reader)

{

Map(reader);

}

#endregion

}

}

issuarez replied on Sunday, March 08, 2015

I tried it both ways and still get the same error:

mCountryList = CountryList.GetAll();

Country mCountry = mCountryList.AddNew();

mCountry.CountryCode = "TST";

mCountry.CountryName = "Test Country";

 

mCountryList =  mCountryList.Save();

Really, I am doing this as a test. In reality I am just trying to edit the object from a grid I have binded to the collection. Every which way I try to save the collection (or mCountry by itself for that matter) I keep getting the same error. I wonder if there is something wrong with the generated CodeSmith classes...            

Copyright (c) Marimer LLC