Serialize Emit Default Values - codefluent

How can I change the EmitDefaultValue in my entities from model?
BOM Producer, even when the dataMember is set to true, is omitting the serialization of null values thus sending incomplete objects to the client application.
[System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=111)]

You can set the value of EmitDefaultValue at project level or property level:
<cf:project cfsm:emitDefaultValue="true"
xmlns:cfsm="http://www.softfluent.com/codefluent/producers.servicemodel/2007/1">
<cf:property cfsm:emitDefaultValue="true"
xmlns:cfsm="http://www.softfluent.com/codefluent/producers.servicemodel/2007/1">

Related

Can not save an entity with the value 0 for a non-nullable numeric property

My model contains an entity Order with a non-nullable property Amount of type decimal:
cf:entity name="Order">
<cf:property name="Id" />
<cf:property name="Amount" typeName="decimal" defaultValue="0" nullable="false" />
</cf:entity>
I can not save an instance of this entity with the value 0 for the property Amount, because when calling "Order.Save()" I get the error "Procedure or function 'Order_Save' expects parameter '#Amount', which was not supplied." from SQL-Server.
Everything goes fine if I give the parameter the default value 0 in the stored procedure:
ALTER PROCEDURE [dbo].[Order_Save]
(
#Amount [decimal] (28, 13) = 0,
...
How can I instruct CodeFluent to generate a stored procedure with the default value 0 for the Amount parameter? Or do you know another solution?
Kind regards
You have to set usePersistenceDefaultValue="false":
<cf:entity name="Order">
<cf:property name="Id" />
<cf:property name="Amount" typeName="decimal" nullable="false" usePersistenceDefaultValue="false" />
</cf:entity>
https://www.softfluent.com/documentation/Properties_DefaultValues.html
The root cause of this is related to Object-relational impedance mismatch that CodeFluent Entities tries to fix.
On the database side you can define a nullable column for an integral type (this is not the case here, but the implications are the same)
On the .NET side, you define an integral type that cannot be null.
To fix the null impedance mismatch between those worlds, CodeFluent Entities defines a 'default value' concept on the .NET side. This follows the Sentinel value pattern.
The .NET side 'default value' for this property will be the .NET value that will be equivalent to null on the database side.
By default, this value is 0 for a number (and -1 for an identity number).
So, when you send a 0 to the database, it's equivalent to sending a null.
In the other way, if you store a null in the database, you'll get a 0 in the .NET property.
Note that unlike most ORMs - CodeFluent Entities is not an ORM but it does contain Object to Relational mapping technology - you will not get an error or an exception because you map nullable column to non nullable .NET types, it'll work like a charm, thanks to this default value concept. It's very practical because in the end, it allows us to map non nullable .NET types to nullable database columns. You don't have to use int? to declare a nullable int column (but you can if you want, CodeFluent Entities supports it).
In your case, since the column is not nullable, you logically get an error if you send a .NET 0.
So, you can choose to not use the 'default value' concept (like in meziantou's answer), or also you can define another default value (like defaultValue="-1" for example, or -2 for an identity column), but you'll still get an error if you send this new default value from .NET.

Property facet Nullable = None is overridden in Model.edmx xml on Update Model from Database

Using Entity Framework 6, I need to maintain a property facet value when using the Model wizard to update.
My property is not nullable in the database, but I have my property facet set as Nullable = None, and my model facet set as Update Property Facets = False. I have to manually edit the Model.edmx file to remove the Nullable="false" attribute from the property. The solution builds successfully in this state. The problem is when I do an Update Model from Database, the Model.edmx file adds back the Nullable="false" attribute and when I build I receive the error that "Error 3031: Problem in mapping fragments starting at line 999:Non-nullable column COLUMN_NAME in table TABLE_NAME is mapped to a nullable entity property."
How do I prevent the Model.edmx from adding the Nullable="false" attribute when running the Model wizard?
This is related to issue Designer: No way to mark facets of entity properties not to be overriden by database facets but slightly different.
I think this is an EF issue, but please note that we are using the NTier Entity Framework (NTIER)
#ChristofSenn Do you have any suggestions?
Apparently this is a known issue with no fix in the near future, as referenced on CodePlex. Property facet Nullable = None is overridden in Model.edmx on Update Model from Database

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.

Using DB-Default values for unmapped bit columns in entity framework 4 (database first)

I am trying to map two concrete entity types and an abstract base type to the same database table.
The table contains a bit column that does not accept null. Column has a default value: ((0)).
Only one of the two concrete entity types (i.e. concrete type 1) needs to use the column's value (for the other (i.e. concrete type 2) it is always false).
I tried adding a property mapped to that column only to the entity type which requires it
and a
When I call SaveChanges I get an UpdateException, with the following message on its inner-most-exception:
"The column cannot contain null values. [ Column name = MY_BIT_COLUMN,Table name = MY_TABLE ]"
I already edited the SSDL section of the EDMX and changed:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" />
to:
<Property Name="MY_BIT_COLUMN" Type="bit" Nullable="false" DefaultValue="false" />
(Without this change mapping failed - will not run)
Is there any way to get around this without adding a property mapped to this column to the second concrete entity type or moving it to the base type?
Adding property as protected to concrete type 2 does work, but I would prefer a more elegant workaround.
If your workaround doesn't work (I'm little bit surprised but it is to late for me to test it now) then the only other workaround is changing your inheritance from TPH to TPT or TPC. The problem obviously is that TPH requires all columns in derived types to be nullable.
Other workaround is making your column member of parent entity and in derived entity use custom constructor which will always set it to false.
The last workaround is making your column nullable and enforce the validation in your business logic for the first entity.

Supporting default column values in custom Entity Framework provider

I'm working on a custom entity framework provider and I need to add support for default column values for this provider. When the user uses the entity framework wizard and selects a table that includes columns with default values, those default values are not being populated into the entity designer.
I'm a little lost on where exactly this population should take place. I believe the appropriate place would be in the GetEdmType method override of DbXmlEnabledProviderManifest but I just don't see how to set the default value, if this is the correct place.
Anybody has experience writing EF providers that support default values for table columns? How do you implement this?
I am a bit late to the party but DbXmlEnabledProviderManifest is not the right place for adding default values. The provider manifest describes capabilities of the database engine itself and is specific (and general) to this database engine and not to a given database and/or table. The default value in the provider manifest tells EF what value to use for the given column property if one is not provided by the user (e.g. if the user user does not specify scale or precision for a decimal column the value from provider manifest will be used for scale and/or precision used for this column).
If you want just to insert a default value for a property the easiest way is to set the property that corresponds to the column on your entity to this value in the constructor. This way the user can always set it to a different value but if s/he does not the default value will be sent to the database. For some corner case scenarios where some of the columns in the database do not have corresponding properties on entities you can use DefaultValue attribute on the Property element in SSDL which will be inserted to the database when you add a row. This is especially useful if those properties are not nullable since without telling EF what value should be inserted EF would try inserting null which would obviously fail for non-nullable columns.