This is a migrated thread and some comments may be shown as answers.

Setting default value for not-null columns

8 Answers 336 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bret
Top achievements
Rank 1
Bret asked on 29 Jun 2011, 10:07 PM
Is there a way to specify a default value for columns which do NOT allow nulls using the designer? I have default values specified in my database schema for most of the fields in question. However, since they are specified as not-null, I cannot create a new instance of the object in question and save it without first specifying all of the not-null fields manually in C#. I haven't found a way, in the designer, to set default values for a field, and it doesn't look like the default values I already have specified in my database schema are being picked up.

For the time being, I've been creating default constructors in my table objects and setting default values there, but if I have to do this for every table that currently uses defaults in the database it is going to take a significant amount of time. I'm also pretty certain this work would get blown away if I had to re-generate the model from my database for some reason. So, I'm wondering if there is a way, in the designer, to enter my default values, or if there is something I need to do to get OpenAccess to recognize the defaults already in my DB schema.

Thanks,

- Bret

8 Answers, 1 is accepted

Sort by
0
Serge
Telerik team
answered on 04 Jul 2011, 04:51 PM
Hi Bret,

 Unfortunately at this time we do not provide such a functionality, we have planned for it however it will not make this release. What we actually would like to do is add a switch that would leave out inserts and updates on certain columns and leave the default generation to the database. 

And yes, if you have had your changes in the code that is generated by the designer the next time you save the rlinq files they will be omitted. However you can create separate files with partial classes to the ones that we have generated for you that hold the default value logic. This way even if the classes are regenerated your code will not be lost.

I hope this is helpful. 

All the best,
Serge
the Telerik team
Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
0
Dan
Top achievements
Rank 1
answered on 27 Feb 2013, 09:50 PM
Serge-

Is there a PITS for this?  I'd like to cast my vote for this functionality as well.
0
Doroteya
Telerik team
answered on 04 Mar 2013, 10:19 AM
Hi Dan,

Currently, Telerik OpenAccess ORM  supports database default values only for the Database First scenario (here is the documentation article about that feature).

There is a post in our Ideas and Feedback portal regarding the extension of that feature, so that it is supported in the Model First scenario. This is the link.

I hope that this is what you are looking for. If it is not or if you have other suggestions, do not hesitate to get back to us.

 

Greetings,
Doroteya
the Telerik team
OpenAccess ORM Q1 2013 is out featuring Multi-Diagrams, Persistent Data Stream Support and much more. Sign up for a free webinar to see all the new stuff in action.
0
John
Top achievements
Rank 1
Iron
answered on 22 Oct 2014, 07:50 PM
I'm facing a similar problem and have gotten a conflicting responses from the documentation (http://docs.telerik.com/data-access/developers-guide/data-access-domain-model/domain-class-mapping/data-access-tasks-define-model-class-mapping-default-values) and from the Telerik rep(http://www.telerik.com/forums/database-default-value-assignment-problem#yg-3jAbi1EqZmmjO_MsYPA). 

Although the columns are designated as having a default and are nullable both in the domain class and the schema's metacolumn, the run-time metadata of the same fields has default value indication as false and the insert statement that's generated has all the fields listed with DBNull for the value. I would appreciate an answer on whether this feature is functional and may be some direction of how to make it work, if it is.

Regards, John
0
Doroteya
Telerik team
answered on 23 Oct 2014, 10:00 AM
Hello John,

I am sorry for both the misleading information in the referred forum thread, and the confusion you experienced.

Let me confirm that the feature is functional, the tutorial in the documentation is correct, and that the settings you applied (according to the description in the referred forum thread) are correct.

The behaviour you observe usually happens when an application is started without explicitly saving the domain model (the .rlinq file) first. On the Data Access side, saving the model is necessary, because it triggers our code generation functionalities, and they take care to preserve the new settings. In this connection, I suggest to you the following:

1. Open the .rlinq file in Visual Designer.
2. Move a class from one place to another on the diagram (in order to make a dummy change).
3. When the asterisk sign (*) next to the name of the model appears, save the model.
4. Run your application for a test.

I hope this helps. Do let us know about the outcome on your side, after applying the suggested workflow.

I am looking forward to your feedback.


Regards,
Doroteya
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
John
Top achievements
Rank 1
Iron
answered on 23 Oct 2014, 02:23 PM
Unfortunately, that didn't work.
This is the appropriate portion of the rlinq file:
Table definition:
      <orm:table name="D3_CALBlock">
        <orm:column name="VFCASC" sql-type="bit" nullable="true" length="0" scale="0" has-default-value="true" ado-type="Bit" />
Domain class definition:
    <DomainClass Id="7e6f56e0-4d4d-477f-9fb4-de69c98be42a" name="D3_CALBlock" namespace="Novatech.ApplicationFramework.CDBService.Contracts" showPropertiesCompartment="true" identityMechanism="DatabaseServerCalculated" concurrencyMode="Changed" updateSchema="true" showNavigationCompartment="true" showImplementationCompartment="true" identityMechanismMember="Id">
 <Property Id="b9852ec5-208a-4296-a2c7-d5713ace1c37" name="VFCASC" type="Boolean" label="VFCASC : Boolean" nullable="true" kind="Persistent" fieldName="_vFCASC" />

Class definition:
   <orm:class name="D3_CALBlock" update-schema="true" uniqueId="7e6f56e0-4d4d-477f-9fb4-de69c98be42a">
        <orm:table name="D3_CALBlock" />
        <orm:identity>
          <orm:key-generator name="autoinc">
            <orm:key-generator-member property-name="Id" />
          </orm:key-generator>
          <orm:single-field field-name="_id" />
        </orm:identity>
        <orm:concurrency strategy="changed" />
        <orm:field name="_vFCASC" property="VFCASC" behavior="readwrite" uniqueId="b9852ec5-208a-4296-a2c7-d5713ace1c37" type="System.Nullable`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]">
          <orm:column name="VFCASC" sql-type="bit" nullable="true" length="0" scale="0" has-default-value="true" ado-type="Bit" />
        </orm:field>


The QuickWatch shows this for the same column during runtime:
- [5] MetaTable: "'D3_CALBlock'" 40 columns Telerik.OpenAccess.Metadata.Relational.MetaTable
+ base MetaTable: "'D3_CALBlock'" 40 columns Telerik.OpenAccess.Metadata.MetaScopedItem {Telerik.OpenAccess.Metadata.Relational.MetaTable}
- Columns Count = 40 System.Collections.Generic.IList<Telerik.OpenAccess.Metadata.Relational.MetaColumn> {Telerik.OpenAccess.Metadata.MetadataCollection<Telerik.OpenAccess.Metadata.Relational.MetaColumn>}
- [0] MetaColumn: "VFCASC" Telerik.OpenAccess.Metadata.Relational.MetaColumn
+ base MetaColumn: "VFCASC" Telerik.OpenAccess.Metadata.MetaItem {Telerik.OpenAccess.Metadata.Relational.MetaColumn}
AdoType -7 int?
+ BackingUDT null Telerik.OpenAccess.Metadata.Relational.MetaUDT
Converter "OpenAccessRuntime.Data.OneZero2BooleanConverter" string
HasDefaultValue false bool
IsBackendCalculated false bool
IsBackendVersion false bool
IsNullable true bool?
IsPrimaryKey false bool
Length 0 int?
Scale 0 int?
SqlType "bit" string
+ Table MetaTable: "'D3_CALBlock'" 40 columns Telerik.OpenAccess.Metadata.Relational.MetaTable
+ TargetClass null Telerik.OpenAccess.Metadata.MetaPersistentType
+ TargetField null Telerik.OpenAccess.Metadata.MetaPrimitiveMember
Telerik.OpenAccess.Data.IDataColumn.AssemblyQualifiedName "" string
Telerik.OpenAccess.Data.IDataColumn.ResolvedLength 0 int?
Telerik.OpenAccess.Data.IDataColumn.ResolvedScale 0 int?
Telerik.OpenAccess.Data.IDataColumn.ResolvedSqlType "bit" string
Telerik.OpenAccess.Data.IDataColumn.UDTName "" string 



I apologize if the formatting is off.

Also, I think the generator breaks when the Int32 of the column is substituted with an enumeration and then made nullable.

Hoping for a speedy response :), John





0
Doroteya
Telerik team
answered on 24 Oct 2014, 02:32 PM
Hi John,

Thank you for the additional information.

In order to implement the scenario when the value for the property could be provided through an enum, you need to specify the enum as nullable when you change the type of the property. This means that you need to add the "?" sign after the name of the enum. For example:

If the enum looks like this:
public enum MyEnum
{
    Zero = 0,
    One = 1,
}

The Type property of VFCASC should be MyEnum?. You can take a look at the attached screenshot.

I hope this helps. Do let us know if you need further information.



Regards,
Doroteya
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
0
John
Top achievements
Rank 1
Iron
answered on 24 Oct 2014, 02:44 PM
That would do it.

Thanks.

John
Tags
Development (API, general questions)
Asked by
Bret
Top achievements
Rank 1
Answers by
Serge
Telerik team
Dan
Top achievements
Rank 1
Doroteya
Telerik team
John
Top achievements
Rank 1
Iron
Share this question
or