I'm using the CSLA.NET 3.5.1 version. I tried changing the RoleList class from Project Tracker to point to my database roles table which uses a Guid for the RoleId instead of integer. When I use the following code in my class I get a InvalidCastException
Unable to cast object of type 'System.Collections.Generic.List`1[Csla.NameValueListBase`2+NameValuePair[System.Int32,System.String]]' to type 'System.Collections.Generic.IList`1[Csla.NameValueListBase`2+NameValuePair[System.Guid,System.String]]'.
using Csla;
using Csla.Data;
using System;
using System.Linq;
namespace ProjectTracker.Library
{
[Serializable()]
public class RoleList : NameValueListBase<Guid, string>
{
#region Business Methods
public static Guid DefaultRole()
{
RoleList list = GetList();
if (list.Count > 0)
return list.Items[0].Key;
else
throw new NullReferenceException("No roles available; default role can not be returned");
}
#endregion
#region Factory Methods
private static RoleList _list;
public static RoleList GetList()
{
if (_list == null)
_list = DataPortal.Fetch<RoleList>();
return _list;
}
/// <summary>
/// Clears the in-memory RoleList cache
/// so the list of roles is reloaded on
/// next request.
/// </summary>
public static void InvalidateCache()
{
_list = null;
}
private RoleList()
{ /* require use of factory methods */ }
#endregion
#region Data Access
private void DataPortal_Fetch()
{
this.RaiseListChangedEvents = false;
using (var ctx = ContextManager<ProjectTracker.DalLinq.AspNetDbDataContext>.GetManager(ProjectTracker.DalLinq.Database.AspNetDb))
{
var data = from role in ctx.DataContext.aspnet_Roles
select new NameValuePair(role.RoleId, role.RoleName);
IsReadOnly = false;
this.AddRange(data);
IsReadOnly = true;
}
this.RaiseListChangedEvents = true;
}
#endregion
}
}
Any help on this matter would be greatly appreciated.
Thanks,
Kent
ktran:Unable to cast object of type 'System.Collections.Generic.List`1[Csla.NameValueListBase`2+NameValuePair[System.Int32,System.String]]' to type 'System.Collections.Generic.IList`1[Csla.NameValueListBase`2+NameValuePair[System.Guid,System.String]]'.
Somewhere in your code you still have a field or class declared as NameValueListBase<int,string> - the exeption is quite clear on that
In fact, it appears that your actual type must be declared as an <int, string> and your field is of <guid, string> - and of course you can't cast the former to the latter.
Thanks for you reply Rocky. To clarify are you saying that the NameValueListBase has to be a
<int, string> pair or can you use it for <guid, string> or whatever combonation of data types? Because there is nowhere in my new project that declares NameValueListBase<int,string>. I've checked the Dbml File for my Linq to Sql classes and the fields I'm using are the expected data types <guid, string>
kent
I’m saying that a NameValueListBase object can be of type
<guid, string>, no problem.
I am also saying that your exception specifically says you have
an object of type <int. string> that you are trying to assign to a field
of type <guid, string>. I don’t know how that is happening in your
code, but that is what the exception is telling you. I suggest using the debugger
to examine the object type and field type to figure out how the object is
ending up as a type you don’t expect.
Rocky
From: ktran
[mailto:cslanet@lhotka.net]
Sent: Thursday, May 08, 2008 2:56 PM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] NameValueListBase using Guid and String instead
of Int and String
Thanks for you reply Rocky. To clarify are you saying that the
NameValueListBase has to be a
<int, string> pair or can you use it for <guid, string> or
whatever combonation of data types? Because there is nowhere in my new
project that declares NameValueListBase<int,string>. I've checked
the Dbml File for my Linq to Sql classes and the fields I'm using are the
expected data types <guid, string>
kent
Thanks Rocky, I got it worked out, stupid mistake I overlooked.
I really appreciate your prompt replies :)
Kent
Copyright (c) Marimer LLC