Object cannot be cast from DBNull to other types Entity Framework - asp.net-mvc-2

I've got a column in one of my models that is nullable. It is nullable in the EDMX, and I've checked that it's nullable in the generated code. I've octuple-checked that it's nullable in the database. However, when I try to save an instance of the model with the column set to null, I receive the exception "Object cannot be cast from DBNull to other types." Most of the code involved here is either the Entity Framework code itself, or generated code. I have other nullable columns that do not seem to have this problem.
Has anyone run into anything like this? Googling around for things mostly reveals people who need to do if (someSqlValue == DBNull.Value) with manual ADO recordsets, but since this is EF interacting with DBNull, there's nowhere in my code that could possibly need to check this.
Unfortunately, I cannot share the code with this, and as I mentioned, most of my nullable columns do not exhibit this problem, so I am not confident in my ability to reproduce the problem in a small test case.

Have you checked that the type in your EDMX matches the type in your database (i.e. that you aren't trying to stuff a null int SQL value into a nullable DateTime field or something like that)?

Related

Setting JPA behavior for importing entities from tables

How can I tell JPA how to behave with different column types in my database when it tries to generate the entities from tables?
For example when I have a column like the following in my MySQL:
`deleted` tinyint(1) NOT NULL DEFAULT 0,
I want in the generated entity by JPA have boolean instead of byte, but what the JPA will generate is something like this:
#Column(nullable=false)
private byte deleted;
However I want to have something like this:
#Column(nullable=false)
#Type(type = "org.hibernate.type.NumericBooleanType")
private boolean deleted;
I think there must be a way that I tell JPA how to translate the column types in my tables in the entities in Java!?
I don't like to modify the entities by hand!
If you're asking how to configure the Eclipse wizard to map TINYINT onto boolean, the answer is you probably cannot.
Using Hibernate tools looks more promising, though. There's a hibernate.reveng.xml config file you can use to control type mapping.
As a side note:
I don't like to modify the entities by hand!
Note that reverse engineering tools in general lack the business knowledge required to generate a business model structure that is completely sensible. You will likely have to do some tweaking (e.g you likely won't get any #ManyToMany associations, even if they are the more natural solution domain-wise).
In the last page of that wizard we can define the expected type for each column. The interesting part is, eclipse stores somehow the selected types for each column and in the future when you try to regenerate the entities you don't need to do this step times to times!

Working with EF for Oracle database. "Schema specified is not valid"

The field in Oracle database is of NUMBER(5), and when I generate EF models from the oracle database, it converts the field into "Int16".
There are some of the rows for that field having values such as "50000", etc, so Int16 is throwing error for obvious reasons.
Now I tried changing it to Int32, and also Decimal in EF models, but it is now giving an error saying -
Member Mapping specified is not valid. The type
'Edm.Decimal[Nullable=True,DefaultValue=,Precision=,Scale=]' of member
'Field1' in type
'MyApp_Models.MyTable' is not compatible
with 'OracleEFProvider.number
Any idea what is wrong here, and what is possible solution of this?
Any help on this much appreciated.
Set your type to just INT instead of INT32. See if that works.

Entity Framework 4.0 & null fields in Views

We're adding some views to our entity framework model. Some fields in these views are nullable datetime2 datatypes in our sql server 2k8 db and the edmx is incorrectly showing these fields as being not null. Is this a known issue?
When I try to change them to not null it still throws the same error - because it appears as if the ssdl is defining it differently?
Has anyone run into this problem?
Edit: My exact error is:
The 'dateTimeAffected' property on
'V_myView' could not be set to a
'null' value. You must set this
property to a non-null value of type
'DateTime'
Removing the view and adding it back in does not fix it as well... still marks the field as not nullable.
Sorry for this silly answer but you did delete the tables and add them again...for some reason ef4 does not refresh them properly...but ya not a known problem with ef4 it should show them as nullable...btw what error are you getting?
you said the property is marked as a key,
but by its name it doesnt sound like this is the case.
(having it marked as key of course dosent sit well with nullablility...)
do you have a PK defined ?

Entity Framework - Mapping decimal(13,0) problem

I'm migrating the aplication of my company (that nowadays run over SQL Server and Oracle) to ASP NET MVC and Entity Framework for persistence.
A create my Entity Model based on SQL Server Database e separately I create a SSDL for Oracle (for Oracle I use DevArt dotConnect for Oracle Provider) and I get some pain troubles.
My table primary keys are on SQL Server are of type decimal(13,0) and on Oracle are number(13,0) but Oracle map it's type to Int64 and SQL Server to decimal, but I need that SQL Server map it to Int64.
I make these modification manually on Entity Data Model and for create records it's works fine, but when I have to delete or update some record I got these error:
The specified value is not an instance of type 'Edm.Decimal'
Parameter name: value
at System.Data.Common.CommandTrees.DbConstantExpression..ctor(DbCommandTree commandTree, Object value, TypeUsage constantType)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateValueExpression(DbCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.GenerateEqualityExpression(DbModificationCommandTree commandTree, EdmProperty property, PropagatorResult value)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildPredicate(DbModificationCommandTree commandTree, PropagatorResult referenceRow, PropagatorResult current, TableChangeProcessor processor, Boolean& rowMustBeTouched)
at System.Data.Mapping.Update.Internal.UpdateCompiler.BuildDeleteCommand(PropagatorResult oldRow, TableChangeProcessor processor)
at System.Data.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler)
Someone can help me?
Why Entity Framework mapping are so fixed? It could be more flexible?
Ps.: The error that I got, I suspect that is because of a association.
I have a Entity named Province and another named Country and I think that the association between these Entities are causing the problem at update and delete.
Regards,
Douglas Aguiar
This may or may not help you, but i had the same error from doing this same thing. So I edited the Conceptual model and change the primary key field from Int32 to Decimal. So far, seems to have fixed things. I still need to test again against Sql Server and make sure this didnt break it.
I was getting the error "The specified value is not an instance of type 'Edm.Decimal' Parameter name: value" as you posted in your question. I had changed the default data types from Decimal to Int32 as this better reflects the true typing. When I first hit this error I rolled back the type changes and was still getting an exception but it changed just slightly but led to further digging. Bottom line, in my scenario we were expecting a trigger to populate the PK during persistence via Before Insert directive. The problem was that the domain class built by EF was setting the PK at 0 so the trigger was never firing as the incoming PK was not null. Of course EF will not let you set the Entity PK to be nullable. Maybe this will help someone else in the future.

Not nullable fields in table inheritance - EDM

I just read this nice article that taught me how to use inheritance (Table-per-hirarchy).
I was wondering, say I have a column 'HireDate' that need to use in the sub-class.
That's for sure that in the DB it has to marked as nullable, but how can I mark it not nullable in the EDM?
I tried to set it as not-nullable, but then it says that it needs a default value, and I want the default value to be DateTime.Now, not a constant value.
If a property (like HireData) is declared on a derived type and you are using TPH you should definitely be able to mark it as non-nullable in the EDM despite the fact it is nullable in the database.
In fact this ability is one of the characteristics of TPH.
...
So I looked at the blog post in question, and noticed it has a problem because it exposes the discriminator column (PersonCategory) as a property of the base Entity, which if allowed would make the type of an Entity mutable, since you could easily do this:
student.PersonCategory = 2;
Which would make Student an Administrator! And that is NOT allowed by the EF.
So if you are following this example closely that is likely to be your problem.
The column that holds the discriminator shouldn't be mapped to a Property in the EDM, it should only be used in the mapping (i.e. 'Add a Condition' under 'Maps to XXX' in the mapping window).
Solution to problem in Blog Post: Remove the PersonCategory property from Person class.
Anyway I hope this helps
Alex