3

I have a

1) text field type in my Sql server that I would like to map it to Entity framework 4.1 Code First. How can I do that?

2) When you declare a string variable in POCO classes, it maps to nvarchar in Sql Server. How it map it to varchar? I am pretty sure my data will always be English and not multilingual.

Thanks in advance :)

Jaggu
  • 6,298
  • 16
  • 58
  • 96
  • 1
    can you not just use `varchar(max)`/`nvarchar(max)` instead of `text`/`ntext`? Then the issue goes away... – Marc Gravell Nov 13 '11 at 11:18
  • Indeed, text/ntext are obsolete and you should be looking to migrate to the "max" types: http://msdn.microsoft.com/en-us/library/ms187993.aspx – Marc Gravell Nov 13 '11 at 11:23

2 Answers2

10

Use this:

public class Foo
{
    [Column(TypeName = "ntext")]
    [MaxLength]
    public string TextProp {get; set;}
}

This should create ntext column in Sql Server and not nvarchar.

TCM
  • 16,780
  • 43
  • 156
  • 254
  • 1
    ntext has been [deprecated in SQL Server 2012](http://msdn.microsoft.com/en-us/library/ms143729.aspx). You could use varchar(max) or nvarchar(max) instead. – sunshineDev May 16 '13 at 18:13
0

I believe you can specify the mapping type of a column with an attribute -

[Column(TypeName = "text")]
public string TextField { get; set; }

You could do this for a varchar also, I found this blog post that contains code that swaps the EF default from using nvarchars to varchars, but I haven't tested the code that is supplied in the post -

http://johncoder.com/Post/EFCodeFirstDisableUnicodeforallStringProperties

ipr101
  • 24,096
  • 8
  • 59
  • 61