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

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)

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:

JPA - Default value

I am developing an JPA application, and I'm using eclipse link provider.
I've a table T1 that has a field F1 defined as not null and with a default value ('U').
I've defined an entity mapped to the table T1. The entity has an attribute mapped to the field F1, and I have configured the attribute with the annotation #ReturnInsert(returnOnly = true).
Documentation says:
Use #ReturnInsert to cause INSERT operations to return values back into the object being written
If I persist an entity with no value in the attribute mapped to F1, everything works fine. The entity is automatically populate with the default value after the insert, and a record is inserted in database.
But if I try to persist an entity with a different value (no default value), a record is inserted into database, but the value of field F1 is the default value, when it should be the value I set in the entity.
Why is happening this? All I want is to change the default value.
Thanks
What I did to have default value for a field in my database was:
Define a default value for that field in database
Make the corresponding attribute in your entity to read-only.
for example for a Date field you can do like this:
in DB:
reg_date DATE NOT NULL DEFAULT CURRENT_DATE
in Entity:
#Basic(optional=false)
#Temporal(TemporalType.DATE)
#Column(name = "reg_date", insertable= false)
private Date regDate;
By doing this I got a field that always be automatically set by database when a new field inserted.

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.

edmx not taking defaults from DB

Here is my scenario. I have a SQL Server database that I am using as the backend. And my application is in ASP.net. I have defined defaults for 2 of the columns in the DB table, table name is Car.
One of the columns in the Car table is called Qty and it's type is Int. I have set a default for this column in the database as 0 (zero)
Another column in the Car table is called Model and it's type is nvarchar(50). I have set a default for this column in the database as *
I want these defaults to show up in the edmx as well. My intention is that when I declare a new instance of class Car in code, I want the default value of Qty to show up as 0 (instead of null) and I want the default value of Model to show up as * (instead of null).
So after doing quite a bit of reading I realized that I need to set StoageGeneratedPattern = Computer for both Qty and Model in the edmx file.
It works perfectly for Qty. When I define a new instance of class Car and look at the properties, I see that Qty is 0. BUT for Model, the default is still null (instead of *).
Here is what I have done. I have set nullable = false and StorageGeneratedPattern = Computed, in the CSDL content and in the SSDL content of the edmx file. I still can't seem to get * as the default value for Model. What am I missing here?

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]