Entity Framework is used to save a lengthy string in a SQL database.

entity-framework sql-server

Question

I'm trying to save the contents of a website to my database using Entity Framework. However, when the length of the HTML > 4000, I get these validation errors:

A first chance exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in EntityFramework.DLL WebDev.WebServer40.exe Information: 0 : Property: RawData Error: The field RawData must be a string or array type with a maximum length of '4000'.

Any idea how to get around this? RawData is being created as NVARCHAR(4000) but a better type would be TEXT. Can I force that somehow?

Thanks!

1
4
10/18/2011 4:22:57 AM

Accepted Answer

The TEXT data type has been deprecated in favor of NVARCHAR(MAX), I would use that to save yourself refactoring down the road.

http://technet.microsoft.com/en-us/library/bb510662.aspx

SQL CE doesn't support NVARCHAR(MAX) so it limits your strings to NVARCHAR(4000)

If it is possible to use SQL Server 2008, Entity Framework will generate NVARCHAR(MAX) columns for you from your strings.

2
10/20/2011 3:40:27 AM

Popular Answer

I'm using SQL CE 4.0 and having the very same problem. I managed to solve it using the following DataAnnotation (i like annotations :) )

public class Page
    {
        [Key]
        public int PageId { get; set; }

        [MaxLength]
        public string Content { get; set; }
    }

Now i can save whatever content in the Content property!



Related Questions





Related

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