How to define a long text field in a custom module

Posted by Community Admin on 05-Aug-2018 18:42

How to define a long text field in a custom module

All Replies

Posted by Community Admin on 17-Mar-2011 00:00

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.

Thanks,
Duneel

Posted by Community Admin on 18-Mar-2011 00:00

Hello Duneel,

You should add this attribute in your model class:

[Database(DBType = "VARCHAR", DBSqlType = "NVARCHAR(MAX)")]

Example:
[DataMember]
        [Database(DBType = "VARCHAR", DBSqlType = "NVARCHAR(MAX)")]
        public virtual Lstring Thumbnail
        
            get
            
                if (this.thumbnail== null)
                    this.thumbnail= this.GetString("Thumbnail");
                return this.thumbnail;
            
            set
            
                this.thumbnail= value;
                this.SetString("Thumbnail", this.thumbnail);
            
        
  
 [Transient]
private LString thumbnail

Posted by Community Admin on 21-May-2011 00:00

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?

Posted by Community Admin on 24-May-2011 00:00

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,
Anton

Posted by Community Admin on 24-May-2011 00:00

Thanks Anton,

That's exactly what we were looking for.

This thread is closed