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
|
I have two tables: UserQuest and UserPreference. The unique id on UserQuest id the same as the unique id on UserPreference. They are defined as well in the model as a one-to-one relationship. My code looks like this to add a new UserPreference: userQuest.UserPreference = new UserPreference();
When I call uow.SaveChanges() this exception is thrown "Object cannot be cast from DBNull to other types." I'm not sure where to go from here. Thanks, Damon |
|
|
I worked around this by creating a one-to-many relationship and adding a new primary key field to the UserPreference table. Then my business logic layer ensures no more than one UserPreference record is added to each User. |
|
|
Both tables must have an Id column (it doesn't technically have to be the primary key ,but it generally will be). In addition, one of the tables must have a foreign key column to the other. From the workaround you describe, it sounds like the issue may have been that UserPreference didn't have an Id column, though I'm not sure why that was causing the error you saw. Once you have an Id column on both tables, and a UserPreferenceId column on the UserQuest (or User?) table (or vice versa depending on how your association is set up), you should be able to go back to defining this as a one-to-one relationship. (If you're using the designer, right-click the one-to-many arrow and choose Convert to One-to-One Association.) Of course, given that you've now got it up and running, you may prefer not to change what works! |
|