How do I tell a property to create a TEXT column instead of a nvarchar? (4000)

c# data-annotations ef-code-first entity-framework sql-server-ce


I'm attempting to find out how to define the column data types that should be formed when the database is automatically constructed while using Entity Framework's Code First capability.

I have an easy 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; }

A SQL CE 4.0 database with the following schema is immediately generated when I launch my application:

DB Schema

All is ok thus far! But the information I'll be entering into theBody and BodyHtml characteristics often exceeds the maximum length permitted for theNVarChar hence I want EF to produce that column typeText for these attributes, columns.

But I can't seem to figure out how to accomplish this! After reading and researching extensively, I attempted to define the column type usingDataAnnotations , based on details from this response:

using System.ComponentModel.DataAnnotations;


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

When the database is destroyed and the application is restarted, this raises the following exception:

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.

I couldn't discover anything that would tell me what namespace or alias I should be providing, so I have no clue what to do.

The annotation was also modified in accordance with this citation:

using System.Data.Linq.Mapping;


[Column(DbType = "Text")]
public string Body { get; set; }

A database was established in this instance, however theBody column continues to beNVarChar(4000) it seems that annotation is disregarded.

Can anyone assist? This sounds like a rather typical need, but I've had no luck finding it.

5/23/2017 12:26:33 PM

Accepted Answer

Although I appreciate the work put into the previous response, I don't believe it adequately answers the topic. I tried this, and I discovered that

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

(That fromSystem.ComponentModel.DataAnnotations ) will function to produce a column of the ntext type.

(My issue with the acceptable response is that it seems to advise changing the column type in the interface while the question is how to do it programmatically.)

2/6/2011 1:38:24 PM

Popular Answer

Utilize the following The largest data type that the database permits will be produced using DataAnnotation and Code-First. In the case of SQL CE, it yields an ntext column as a result.


or by utilizing the fluent API for EF 4.1 RC...

protected override void OnModelCreating(ModelBuilder modelBuilder){
        .Property(p => p.Body)

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow