InvalidCastException at LoadSimpleValuesByIndex

Jul 31, 2008 at 7:40 PM
Hi there. I'm having a problem that I can't seem to track down. When I try to pull an object with the following definition from a database, I'm getting an InvalidCastException.

The object definition:
    [DbTable("SampledPoint")]
    public class SampledDataORM : DbObjectModel<SampledDataORM>
    {
        [DbColumn("StartTime")]
        public DateTime sampleStartTime;

        [DbColumn("EndTime")]
        public DateTime endTime;

        public SampledDataORM(DateTime startTime, DateTime endTime)
        {
            this.sampleStartTime = startTime;
            this.endTime = endTime;
        }
    }

and the code I use to create and get it:

    SampledDataORM test = SampledDataORM.New(DateTime.Now, DateTime.Now);
    test.Save();
    SampledDataORM lastEntry = SampledDataORM.FindById(test.Id);

    Console.WriteLine(lastEntry.Id);
    Console.ReadLine();

throws the exception:

Unable to cast object of type 'Lephone.Data.Definition.DbObjectModel`1[SampledDataORM]' to type 'SampledDataORM'

when I attempt to retrieve the record, but does save it correctly.

Am I just doing this wrong?




Coordinator
Aug 1, 2008 at 12:42 AM

    [DbTable("SampledPoint")]
    public abstract class SampledDataORM : DbObjectModel<SampledDataORM>
    {
        [DbColumn("EndTime")]
        public abstract DateTime endTime { get; set; }

        [DbColumn("StartTime")]
        public abstract DateTime sampleStartTime { get; set; }

        public SampledDataORM()
        {
        }

        public SampledDataORM(DateTime startTime, DateTime endTime)
        {
            this.sampleStartTime = startTime;
            this.endTime = endTime;
        }
    }

Aug 1, 2008 at 2:21 AM
Thanks! Looks like I need to spend some more time diving into the code, because I've never seen such a technique used before.
Aug 1, 2008 at 6:31 AM
Hey, if you wouldn't mind satisfying my curiosity, can you give a little background on why you chose to use the Reflection classes to create the concrete classes dynamically rather than using concrete class implementations as I was trying to do? I can't see why it is preferable to have the user define an abstract class and have the code create the concrete implementation rather than having the user define the concrete implementation and use the attributes to determine how to fill in the information from the database. Is there something you gain by using abstract class definitions? And, while I'm asking, how is it that you can create a concrete class from an abstract class? I'm assuming that they are in fact not the same class at all (from the InvalidClassException I was getting)
Coordinator
Aug 1, 2008 at 1:39 PM
http://www.codeplex.com/DbEntry/Wiki/View.aspx?title=Dynamic%20object&referringTitle=Home
http://www.codeplex.com/DbEntry/Wiki/View.aspx?title=Partial%20update&referringTitle=Home

Hope the articles could answer your questions.
Aug 2, 2008 at 12:04 AM
That does help, thank you. It's a really interesting way of doing it--I was just so confused as I've not spent much time looking at the Reflection classes. I'm impressed with how powerful they are.