+1-888-365-2779
Try Now
More in this section

Forums / Sitefinity SDK / How to define a long text field in a custom module

How to define a long text field in a custom module

5 posts, 0 answered
  1. Duneel
    Duneel avatar
    166 posts
    Registered:
    08 Dec 2010
    17 Mar 2011
    Link to this post
    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
  2. jocelyn payneau
    jocelyn payneau avatar
    51 posts
    Registered:
    07 Dec 2009
    18 Mar 2011
    Link to this post
    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
  3. Seattle Web Group
    Seattle Web Group avatar
    29 posts
    Registered:
    03 May 2011
    21 May 2011
    Link to this post
    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?
  4. Anton Mernov
    Anton Mernov avatar
    110 posts
    Registered:
    03 Dec 2008
    24 May 2011
    Link to this post
    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
  5. Seattle Web Group
    Seattle Web Group avatar
    29 posts
    Registered:
    03 May 2011
    24 May 2011
    Link to this post
    Thanks Anton,

    That's exactly what we were looking for.
5 posts, 0 answered