entity framework working with entity properties that are null - entity-framework

In Entity framework, I am having entity properties which correspond to SQL Table columns which are ALLOW NULL = True in Database. When I use those properties in C#, if the value is null it gives exception. So how to handle this in EF, this happens for all the table columns that allow null.

I am assuming these columns are Nullable type in your .NET code, since they have ALLOW NULL = True in database. So, everytime you use these values, check for PropertyName.HasValue() which returns false whenever the property is null.

Related

EF - eliminating nullable fields without changing database

We have a large legacy transactional-type SqlServer database with hundreds of tables, and most of the fields in these tables have the Allow Null attribute turned on.
We are also using Entity Frameworks and are developing a new C# to access the database. In the POCO classes generated by EF, the field properties have, of course, the Nullable decorator, which produces nullable data types (e.g., "int ?").
The impact is that if any given field contains null and we don't explicitly check for null before accessing the field, the app will blow up. Each field also has to be casted for use, or the Value property has to be used.
We could, of course, changes the schema and turn off the Allow Null property, but are getting push-back from the DBA's who want to avoid making any changes.
Is there a way in the EF generator to turn off the Nullable attribute for some or all fields, and coerce the logic to return default values for null fields (e.g., 0 for int, false for bool, etc.) on select statements, and to use default values for inserts/updates?

EF Table per Hierarchy (TPH) not saving because can't insert value null in Discriminator column

I have a table used for multiply type of category and it contains a Discriminator column named 'ClassName' to specify the type of object to load. The ClassName column is non nullable with a default value of 'Category'
My problem is when saving a new item , I get the error :
'Cannot insert the value null into column ClassName' table Category.
I tought that ef would set the ClassName value base on the new object class.
How can I save my object with the right 'ClassName' value ?
I changed my db Structure to accept null. EF will set null if the object name match the table name and will set the discreminator name for the derived classes.
It's old question but I just met it today. In .net 4.0 code first I don't meet "disclaimer column" but when downgrade to net 3.5, it make me tired for a day.
This is my solution
Change the column in the database to ALLOW NULL (using alter table)
and update the edmx file to make it update the change(allow null)

Entity Framework Nullable Types not included in the CreateXXX Method

I'm using Entity Framework 4.0. I have tables with many nullable types. For a table XYZ the Entity Framework creates an object with an insert method named CreateXYZ, but this method only includes the parameters for non-nullable database fields.
Nullable fields can therefore not be set on the initial insert.
What is the best way to create an CreateXYZ method that can take the nullable types as well?
The nullable values are not included in the CreatedXYZ. You can assign them later.

How do I get Entity Framework to only update the Properties modified in the SQL generated?

I am using Entity Framework with the Self-Tracking Entity T4 templates, which by default will generate a SQL Query setting all the properties on the Entity in an UPDATE statement. I only want an UPDATE statement that contains the properties modified.
I modified the T4 Template as specified in the book: Entity Framework Recipes: A Problem-Solution Approach page 503.
I changed to this line in the T4 Template:
OriginalValueMembers originalValueMembers = new OriginalValueMembers(false, metadataWorkspace, ef);
Making the Entity track each property change instead of just tracking that the Entity changed.
And also
context.ObjectStateManager.ChangeObjectState(entity, EntityState.Unchanged);
After making these changes I got the desired result of SQL statement with only the modified values/properties in the UPDATE statement. However, there was a strange side effect. When updating a nullable INT property from null to something other than null, the change of that was ignored by Entity Framework. The Self-Tracking Models show the change in the ChangeTracker with the accurate OriginalValue null, but when Entity Framework tried to generate the UPDATE SQL it did not see that property change if the original value null and the new value is not null. I worked if the original value was not null and value gets changed.
It seems to work ok on a string property going from null to a non-null value, but int? is not working.
Does anyone have any ideas?
If helpful, have found post that fixes this: fix update of nullable column.

How do I get the entity framework to stop setting the rowversion field?

Im using the Entity Framework and I have a rowversion (timestamp) field on a table to use for concurrency. However, when updating the entity object, it keeps trying to set the rowversion column to null, and I get an error:
The 'VerCol' property on 'LmpDemoRequest' could not be set to a 'null' value. You must set this property to a non-null value of type 'Byte[]'.
I have the VerCol column within the entity definition, but I am unable to remove the "Setter" function.
How do I get the entity framework to stop attempting to set this column?
You can pass any arbitrary, valid values for the RowVersion fields (DateTime.Now for example). They will be overwritten with the server-generated values.
For future releases of EF, there should be support for "shadow properties", which exist in the model but not in your classes. That feature would be useful in situations such as this.
I had a case where a view included a RowVersion column from a table that was left joined in the view... so that column could be null sometimes.
But EF4 'knows' that a RowVersion column cannot be null, so even in a simple LINQ query, it was throwing an InvalidOperationException:
The 'PersonRowVersion' property on 'vVoteInfo' could not be set to a 'DBNull' value. You must set this property to a non-null value of type 'Byte[]'
I finally had to change the view to use this for the RowVersion column, so that EF would be happy:
coalesce(p._RowVersion, cast(0 as binary(6))) [PersonRowVersion]