I am working with the Code First function in the Entity Framework, and I am trying to figure out how I can specify the data types of the columns that should be created when the database is automatically 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, the SQL CE 4.0 database is automatically created with the following schema:

So far so good! However, the data that I will insert in the Body and BodyHtml is usually greater than the maximum length for the NVarChar column NVarChar , so I want EF to generate Text columns for these properties.
However, I cannot find a way to do this! After quite a bit of talking and reading, I tried to specify the type of column using DataAnnotations from the information found in this answer :
using System.ComponentModel.DataAnnotations; ... [Column(TypeName = "Text")] public string Body { get; set; }
This throws the following exception (when deleting the database and re-executing the application):
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 indicate, and I could not find anything that could tell me.
I also tried changing the annotation to this link :
using System.Data.Linq.Mapping; ... [Column(DbType = "Text")] public string Body { get; set; }
In this case, the database is created, but the Body column is still NVarChar(4000) , so it seems that the annotation is ignored.
Can anyone help? It seems like this should be a fairly common requirement, but my search turned out to be fruitless!
c # sql-server-ce entity-framework ef-code-first data-annotations
Mark Bell 04 Feb '11 at 7:13 2011-02-04 07:13
source share