How to define a long text field in a custom module - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

How to define a long text field in a custom module

  • How to define a long text field in a custom module
  • Hi,

    I have built a custom module based on the products module. Now I want to add a new field to the module that is capable of saving some lengthy text. In other words the field should create a NVARCHAR(MAX) column in the database. but the backend views should show it as a normal text field.

    How should I achieve this? What files do I have to modify in my module and what data type I have to use? I tried with string fields, but it creates only a VARCHAR(255) column in the database which is not long enough.

  • Hello Duneel,

    You should add this attribute in your model class:
    [Database(DBType = "VARCHAR", DBSqlType = "NVARCHAR(MAX)")]

            [Database(DBType = "VARCHAR", DBSqlType = "NVARCHAR(MAX)")]
            public virtual Lstring Thumbnail
                    if (this.thumbnail== null)
                        this.thumbnail= this.GetString("Thumbnail");
                    return this.thumbnail;
                    this.thumbnail= value;
                    this.SetString("Thumbnail", this.thumbnail);
    private LString thumbnail
  • Hi Jocelyn,

    I'm running into some trouble relating to database field definitions.  When I use the [Database(DBType = "VARCHAR", DBSqlType = "NVARCHAR(MAX)")] attribute as you've described I get the compiler warning "warning CS0618: 'Telerik.Sitefinity.DatabaseAttribute' is obsolete: 'Use DatabaseMappingAttribute instead.'" and the code does not work.

    I was able to use [MetadataMapping(true, true)] as an alternative but it would only work for the Lstring data type.  Is there something available that will do this for the string data type?  The Lstring data type is overkill for what's being stored in some of the fields we're using as they don't require localization.

    What would the process be for setting up a field with the TEXT or NTEXT data type rather than the NVARCHAR data type?

    Would the module installer change the text fields back to VARCHAR(255) when a module is updated if we ran a script to manually adjust the data types after the module has initially been installed?
  • Hi Seattle,

    I'm using fluent mapping in my custom module and the following approach works for me fine:

    MappingConfiguration<CustomItem> customConfiguration = new MappingConfiguration<CustomItem>();
    customConfiguration .HasProperty(c => c.Id).IsIdentity();
    customConfiguration .HasProperty(c => c.DateCreated);
    customConfiguration .HasProperty(c => c.Description).IsLongText(this.Context).IsNullable();

    After module installation, the Description field in database has a type NVARCHAR(MAX)

    I hope this helps.

    Best regards,
  • Thanks Anton,

    That's exactly what we were looking for.