This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Model Code: using System; } Model Definition
<?xml version="1.0" encoding="utf-8"?> ![]()
|
|
|
I assume you are getting the "specified cast not valid" when you load data into the model from the database (i.e. not from the designer)? If that is correct, could you post a copy of your database schema, and which database provider you are using? Also it would be useful if you could post the stack trace of the InvalidCastException. Thanks! |
|
|
Actually it was going the other way, trying to update the database, though I guess it probably happens either way. Here is the schema though:
SET FOREIGN_KEY_CHECKS=0;
|
|
|
I'm afraid I'm having difficulty reproducing your problem. At the moment, your database schema as posted is out of sync with your model because it does not contain the parent-child relationship between MenuEntries (i.e. a ParentEntryId column and associated foreign key). When I run Update Database, I get a message telling me that the MySQL provider doesn't support adding references (we don't have this feature for MySQL yet, sorry). If I go ahead and run the LightSpeed code anyway, when I try to load or save records, I get a MySqlException that the ParentEntryId doesn't exist in the MenuEntries table. If I add ParentEntryId (and the associated foreign key) to the database by hand, and then run, it seems to load the data okay for me, and I can add entries to the database successfully as well. I did notice in your model that _parentEntryId is an int rather than a Nullable<int>, which will cause problems because _parentEntryId is a foreign key to the same table, and the parenting reference needs to top out somewhere. (To fix this, set the association's IsNullable property to true.) But LightSpeed throws a specific exception for this rather than a "specified cast is not valid." Also the Parent column in the database is non-nullable, but the Parent property in the model is nullable. But again, if I fail to set the Parent before saving, this causes a MySqlException rather than a "specified cast is not valid." Would it be possible for you to post the following for me: * .lsmodel file (*not* just the generated .cs file) You can attach files via the Options tab. Sorry to be a pain, but I'm not having any luck reproducing the problem so far and these will really help. Thanks! |
|
|
Oh, and if the error is occurring when you add entities to the DB, what IdentityMethod are you using? The default is IdentityMethod.KeyTable which requires a table called KeyTable in the DB, but again if this is missing then the error is "missing table" rather than "invalid cast." If you are using a KeyTable, can you include the schema for that? Thanks! |
|