How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

55,894

Solution 1

I appreciate the effort that went into the existing answer, but I haven't found it actually answering the question... so I tested this, and found out that

[Column(TypeName = "ntext")]
public string Body { get; set; }

(the one from System.ComponentModel.DataAnnotations) will work to create an ntext type column.

(My problem with the accepted answer is that it seems to indicate that you should change the column type in the interface, but the question is how to do it programmatically.)

Solution 2

You can use the following DataAnnotation and Code-First will generate the maximum sized data type that the database allows. In the case of Sql CE it results in an underlying ntext column.

[MaxLength]

or using the EF 4.1 RC fluent API...

protected override void OnModelCreating(ModelBuilder modelBuilder){
    modelBuilder.Entity<Article>()
        .Property(p => p.Body)
        .IsMaxLength();
}

Solution 3

Have you tried ntext? I have just created a SQL CE 4.0 database, and when I manually add a column to a table, I notice that text isn't available in the list of datatypes while ntext is. Just like you can pick nvarchar but no varchar.

Unfortunately, the biggest nvarchar size you can choose is 4000. So nvarchar(max) is also not an option.

There is ntext but no text

Solution 4

The issue with using the string length attribute e.g.

[StringLength(4010)]

Is that any string > the number of chars defined in the attribute will trigger a validation exception, which kind of goes against any reason why you would use a non defined field size on a column, or you use a huge number in the attribute and lose any validation offered by the attribute. Ultimately you are using a validation mechanism to solve a mapping issue if you use the StringLength attribute, where as Marcel Popescu's answer using the Column attribute is a much better solution as it is using the mapping attributes to define type, and still allows you to use the StringLength attribute for validation.

Another option is to use the EF4 CTP5 fluent API and define the column mapping in the OnModelCreating event in the DbContext e.g.

protected override void OnModelCreating(ModelBuilder modelBuilder){
    modelBuilder.Entity<Article>()
    .Property(p => p.Body)
    .HasColumnType("nvarchar(max)");
}

Also it should be noted that NText is a deprecated data type (ntext, text, and image (Transact-SQL) MS Books Online) and the recommendation is to use NVarChar(MAX) in its place

Solution 5

I know, this is probably a year too late but:

Use:

[StringLength(-1)] 

This will create a nText field. I managed to store at least 25Kbytes in that field using the Compact Edition 4.0 database.

Share:
55,894
Mark Bell
Author by

Mark Bell

I'm a web developer based in Devon, UK.

Updated on May 20, 2020

Comments

  • Mark Bell
    Mark Bell almost 4 years

    I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated.

    I have a simple model:

    public class Article
    {
        public int ArticleID { get; set; }
    
        public string Title { get; set; }
        public string Author { get; set; }
        public string Summary { get; set; }
        public string SummaryHtml { get; set; }
        public string Body { get; set; }
        public string BodyHtml { get; set; }
        public string Slug { get; set; }
    
        public DateTime Published { get; set; }
        public DateTime Updated { get; set; }
    
        public virtual ICollection<Comment> Comments { get; set; }
    }
    

    When I run my application, a SQL CE 4.0 database is automatically created with the following schema:

    DB Schema

    So far, so good! However, the data I will be inserting into the Body and BodyHtml properties is routinely larger than the maximum allowed length for the NVarChar column type, so I want EF to generate Text columns for these properties.

    However, I cannot seem to find a way to do this! After quite a bit of Googling and reading, I tried to specify the column type using DataAnnotations, from information found in this answer:

    using System.ComponentModel.DataAnnotations;
    
    ...
    
    [Column(TypeName = "Text")]
    public string Body { get; set; }
    

    This throws the following exception (when the database is deleted and the app is re-run):

    Schema specified is not valid. Errors: (12,6) : error 0040: The Type text is not qualified with a namespace or alias. Only PrimitiveTypes can be used without qualification.
    

    But I have no idea what namespace or alias I should be specifying, and I couldn't find anything that would tell me.

    I also tried changing the annotation as per this reference:

    using System.Data.Linq.Mapping;
    
    ...
    
    [Column(DbType = "Text")]
    public string Body { get; set; }
    

    In this case a database is created, but the Body column is still an NVarChar(4000), so it seems that annotation is ignored.

    Can anyone help? This seems like it should be a fairly common requirement, yet my searching has been fruitless!

  • Mark Bell
    Mark Bell over 13 years
    I have tried that, and it doesn't make any difference: the generated column is still a varchar. +1 for the useful discussion link though, seems like there are some other methods I could try—I'll have a read through.
  • LordHits
    LordHits about 13 years
    This will not work with SQL CE. See Michael's answer on here in using [MaxLength] instead.
  • Nestor
    Nestor almost 13 years
    Model validation still causes troubles and raises an error when your try insert more than 4,000 chars, even database is set to ntext, but seems when it reads "string" type, it automatically guess max length=4000, ignore the type at the database. For me still not solved.
  • Nestor
    Nestor almost 13 years
    But how you define your model, so model validation understands is a ntext, and not a string (with default max = 4,000). I have set ntext to my db, and still get error when adding more than 4,000 chars. My model property is string type.
  • Nestor
    Nestor almost 13 years
    For MS SQL CE is not deprecated, here the question mention the case for MS SQL CE (4).
  • Kristof Claes
    Kristof Claes almost 13 years
    Have you tried putting [Column(TypeName = "ntext")] on the string property?
  • Nestor
    Nestor almost 13 years
    Yes, but was not enough, I added [MaxLength] to force check field size in database instead of default string type max length, and now is ok.
  • nakchak
    nakchak almost 13 years
    True, just checked the Sql CE 4.0 Docs (msdn.microsoft.com/en-us/library/ms172424%28v=SQL.110%29.as‌​px) however the ntext type cant be used with any (sql server) string functions, and it stores data in a separate data page to the rest of the table, so personally unless i had a very clear reason not to i would stick with using nvarchar(max) for performance, and functionality reasons
  • Rich
    Rich over 9 years
    For anyone still struggling with this under SQL Server CE + EF code-first, this is the solution you're looking for if you tried typeName=ntext and still got the error "the field must be a string or array type with a maximum length of '4000'" with data length >4000... [MaxLength] is all you need on the column to make it ntext and allow the full data length to be used