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

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]

Related

How to create RowVersion column for optimistic concurrency using model first approach?

I am working on a legacy web app which implements model first(edmx file) approach using Entity Framework.
I need to implement optimistic concurrency, so I have added this field as following:
and inside the database has been created as binary(8) type.
But when I try to update the entity is getting updated but the VersionRow values is not updated(no new value generated).
P.S
When I added the column I have binded default value as 0x0000000000000000 because it does not allow null values.
Yea I solved it this way:
1) I changed the type of RowVersion column from Binary(10) into timestamp inside SqlServer.
2) In the property details inside the .edmx file I have put the property StoreGeneratedPattern of the property RowVersion as Computed.
Computed it means that a new value is generated on insert and update.
Now it became as following:

entity framework working with entity properties that are null

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.

Why does the `DatabaseGeneratedOption.None` exist?

This is used with the Property(() => p).HasDatabaseGeneratedOption() call. Is is perhaps to turn off default DB value generation?
EF uses DatabaseGeneratedOption to figure out what to do with the value of a key column for new entities. If the DatabaseGeneratedOption is Identity EF knows that the value the property is set to can be ignored and that the one that comes from the database should be used. If the DatabaseGeneratedOption is None EF will insert the value of the property to the database as the value of the key column.
In Code First - when Code First conventions find an int property that can be the key property for the given entity they by default will configure this column as identity column (meaning the database will generate the value of the key column/property). DatabaseGeneratedOption.None allows you to overwrite this if you want to set key values on your own.
Its effect is to configure EF to not fetch a new identity value after inserting into the database.

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)

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.