Create typed List of Entity from DataTable Error

Posts   
 
    
gdantonio
User
Posts: 2
Joined: 02-Aug-2007
# Posted on: 07-Aug-2007 14:10:19   

asp.net 2.0 llblgenpro 2 VS 2005 SQL SERVER 2005 Hi, I've tried to create a function that populate a list of Class Entity from DataTable using .NET reflection but i've got this error:

SD.LLBLGen.Pro.ORMSupportClasses.ORMFieldIsReadonlyException: The field 'ProductID' is read-only and can't be changed

Here is a peice of my code:

    public static List<T> FillCollection(System.Data.DataTable dtSource)
    {
        List<T> retCollection = new List<T>();
        foreach (System.Data.DataRow row in dtSource.Rows)
        {
            object element = Activator.CreateInstance<T>();
            element = SetItem((T)element, row);
            retCollection.Add((T)element);
        }

        return retCollection; 
    }

    private static object SetItem(T item, System.Data.DataRow row)
    {

        Type type = item.GetType();

        foreach (DataColumn column in row.Table.Columns)
        {
            PropertyInfo property = type.GetProperty(column.ColumnName);
            if (property != null && property.CanWrite)
            {
                object value = row[column.ColumnName];
                property.SetValue(item, value, null);
            }
        }
        return item;
    }

SetItem throw an exception on property ProductID the PK of the table that i want to map in the list.

Any suggestion??

Thanks in advance

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 07-Aug-2007 19:09:46   

What if you use the Class-Constructor of <T> that receives the PK (_IEntity.PrimaryKeyFields_)?

gdantonio
User
Posts: 2
Joined: 02-Aug-2007
# Posted on: 08-Aug-2007 08:04:50   

Thanks. But Could you give me an exmple?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 08-Aug-2007 19:25:29   

I think you should use the build-in projection code simple_smile Please see the example in Using the generated code -> Adapter / Selfservicing -> Using entityviews with entity collections -> Examples of EntityView2 projections

it shows you how to project a view (which is available to you on every entity collection by its DefaultView property, or you can create a new one) onto a datatable simple_smile

Frans Bouma | Lead developer LLBLGen Pro