I am trying to create a wrapper class for an entity class.
Everything works fine until I try to get a collection.
When I run this code:
public IList<User> GetUsers()
{
try
{
this.SetConnection();
EntityCollection<User> users = new EntityCollection<User>();
this._DataAccessAdapter.FetchEntityCollection(
users,
null,
0
);
return users;
}
catch (Exception exception)
{
throw new Exception(
"An error occured while getting all users.\nCheck inner exception.",
exception
);
}
}
I get this error:
"entityToAdd isn't of the right type"
I realize what the problem is and why it it happening.
How do I fix it?
I have searched the forum and have found similar threads but not an answer that does not involve reading the SDK documentation (which I don't have).
Here is the entire class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ABS.BusinessObjects.EntityClasses;
using ABS.BusinessObjects.DatabaseSpecific;
using ABS.BusinessObjects.HelperClasses;
using System.Data;
using ABS.BusinessObjects.FactoryClasses;
namespace BusinessObjects
{
public class User : UserEntity
{
private string _ConnectionString;
public string ConnectionString
{
get
{
return this._ConnectionString;
}
set
{
this._ConnectionString = value;
}
}
private DataAccessAdapter _DataAccessAdapter = null;
public DataAccessAdapter DataAccessAdapter
{
get
{
return this._DataAccessAdapter;
}
set
{
this._DataAccessAdapter = value;
}
}
public User()
: base()
{
}
public User(
string connectionString
)
: base()
{
this._ConnectionString = connectionString;
}
public User(
string connectionString,
string userID
)
: base(userID)
{
this._ConnectionString = connectionString;
}
~User()
{
if (this.DataAccessAdapter != null)
{
this.DataAccessAdapter.CloseConnection();
this.DataAccessAdapter.Dispose();
this.DataAccessAdapter = null;
}
}
public void SetConnection()
{
if (this._DataAccessAdapter == null)
{
this._DataAccessAdapter = new DataAccessAdapter(this._ConnectionString);
}
}
public IList<User> GetUsers()
{
try
{
this.SetConnection();
EntityCollection<User> users = new EntityCollection<User>();
this._DataAccessAdapter.FetchEntityCollection(
users,
null,
0
);
return users;
}
catch (Exception exception)
{
throw new Exception(
"An error occured while getting all users.\nCheck inner exception.",
exception
);
}
}
public void Save()
{
try
{
this.SetConnection();
this._DataAccessAdapter.StartTransaction(
IsolationLevel.ReadUncommitted,
"SaveUser"
);
this._DataAccessAdapter.SaveEntity(this, true);
this._DataAccessAdapter.Commit();
}
catch (Exception exception)
{
if ((this._DataAccessAdapter != null) &&
(this._DataAccessAdapter.IsTransactionInProgress))
{
this._DataAccessAdapter.Rollback();
}
throw new Exception(
"An error occured while saving user.",
exception
);
}
}
public void Delete()
{
try
{
this.SetConnection();
this._DataAccessAdapter.StartTransaction(
IsolationLevel.ReadUncommitted,
"DeleteUser"
);
this._DataAccessAdapter.DeleteEntity(this);
this._DataAccessAdapter.Commit();
}
catch (Exception exception)
{
if ((this._DataAccessAdapter != null) &&
(this._DataAccessAdapter.IsTransactionInProgress))
{
this._DataAccessAdapter.Rollback();
}
throw new Exception(
"An error occured while deleting user.",
exception
);
}
}
}
}