How specify the identity seed for a property in the CodeFluent model? - codefluent

All the key properties in my CodeFluent model are of type ulong and must be automatically incremented by the database, for example:
<cf:property name="Id" typeName="ulong" key="true" persistenceIdentity="true" cfps:hint="CLUSTERED" />
One specific key property must start with the value 10 instead of 1.
How can I specify this?
Another question: Do you have documentation about the cfps namespace?

Using the latest version of CodeFluent Entities (>=836) you can set the identity seed and increment at property level:
<cf:property name="Id" typeName="int"
cfps:identitySeed="10"
cfps:identityIncrement="2"
xmlns:cfps="http://www.softfluent.com/codefluent/producers.sqlserver/2005/1"/>

Related

Is there an equivalent to updateSchema="false" that applies to individual entities?

Setting updateSchema="false" in the configuration section of the XML file for the SQL producer will prevent Codefluent from altering the schema (i.e. field names, field type, indexes, etc) of the SQL table while still allowing for the creating of stored procedures and methods against the table. This is discussed in the blog article https://blog.codefluententities.com/2011/10/31/interoperate-with-an-existing-database-using-codefluent-entities/
Question
Is there anything built in that would achieve the same effect but apply only apply to the underlying tables of specific entities?
You can disable table diffs generation for a specific entity using an attribute:
<cf:entity name="Customer" cfps:produceTableDiff="false">
<cf:property name="Id" key="true" />
</cf:entity>

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.

Entity Framework EDMX Diagram table missing properties after update

I'm maintaining an asp.Net MVC 3 web application using Entity Framework 4.1 Database first approach. I have had to add two new properties to one of the tables in the database.
I then right clicked on the EDMX diagram and choose 'Update Model from Database' within the context menu, like so:
This works without any problems, but the two new fields aren't added to the expected table. However, when I open the EDMX file in XML format, I can see the two new fields listed, like so:
<EntityType Name="Shift">
<Key>
<PropertyRef Name="shiftID" />
</Key>
<Property Name="shiftID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
//Two new fields below
<Property Name="shiftTitleGradeID" Type="int" />
<Property Name="shiftTitleSubID" Type="int" />
</EntityType>
Can anyone advise me on how to get the two new fields into my EDMX diagram and not just the XML file?
Thanks in advance.
Got it fixed. I had to delete all references to the new properties in the XML file. Then recreate the update model from database procedure, this time ensuring the two checkboxes where ticked, Pluralize or singularize generated object names, and, Include foreign key columns in the model (I thought I had these ticked the first time, but I didn't).
This fixed my problem.

EF5: Multiple result sets from stored procedure

I followed this article to retrieve multiple entity sets from a stored procedure.
I have altered the column mappings of my entities so they pass through Resharper.
Do I have to rewrite the column mappings in the result mapping?
For example I have the following EntitySetMapping:
<EntitySetMapping Name="IrmaObjectConfiguraties">
<EntityTypeMapping TypeName="IrmaModel.IrmaObjectConfiguratie">
<MappingFragment StoreEntitySet="IrmaObjectConfiguratie">
<ScalarProperty Name="Gid" ColumnName="GID" />
<ScalarProperty Name="IrmaObjectGid" ColumnName="IrmaObject_GID" />
<ScalarProperty Name="IrmaConfiguratieGid" ColumnName="IrmaConfiguratie_GID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
This entity is returned bij as part of the stored proc result sets:
<ResultMapping>
<EntityTypeMapping TypeName="IrmaModel.IrmaObjectConfiguratie">
<!--ScalarProperty Name="Gid" ColumnName="Gid" />
<ScalarProperty Name="IrmaObjectGid" ColumnName="IrmaObject_Gid" />
<ScalarProperty Name="IrmaConfiguratieGid" ColumnName="IrmaConfiguratie_Gid" /-->
</EntityTypeMapping>
</ResultMapping>
When I remove the remarks the execution of the imported function goes ok, but when there is no column mapping I retrieve the following error:
The data reader is incompatible with the specified
'IrmaModel.IrmaObjectConfiguratie'. A member of the type,
'IrmaObjectGid', does not have a corresponding column in the data
reader with the same name.
As a workaround I could define all column mappings again, but is is also possible to use the column mappings that are defined in the EntitySetMapping?
I don't think ResultMapping re-uses mappings from EntitySet mappings. Seems (I am not 100% positive here) that if you don't specify property <-> column mappings EF will try to use property names as column names in the resultset returned by your stored procedure.

Distinguishing between storage model and conceptual model field names (Entity Framework)

Every sample I come across has the entities and properties in the storage model named exactly the same as in the conceptual model. So in the mapping section, I can't tell whether an entity or property is from the storage model or conceptual model.
This is a snippet of an Entity Framework diagram. Which "ID" fields are from the database and which are from the entities?
<AssociationSetMapping Name="FK_Orders_Customers" TypeName="ContosoModel.FK_Orders_Customers" StoreEntitySet="Order">
<EndProperty Name="Customer">
<ScalarProperty Name="ID" ColumnName="CustomerID" />
</EndProperty>
<EndProperty Name="Order">
<ScalarProperty Name="ID" ColumnName="ID" />
</EndProperty>
</AssociationSetMapping>
Well only databases have Columns, so ColumnName is the Database Name.
Name is from the Entity (or in this case the Association).
Hope this helps
Alex