2013-02-13 57 views
2

我的代码工作了一个多月,我的应用程序解析RSS源(http://www.whitehouse.gov/feed/blog/white-house),并插入消息到DB:Wp的插入分贝例外

今天我当应用程序试图添加该消息的”第一异常女士的盒子在2013年的状态“的分贝。这里是我的代码:

News item = Query.instance().AddNews(channel.Guid, channel.Description, channel.Link, channel.PublishDate, channel.Title); 

    public News AddNews(string guid, string description, string link, DateTime publishDate, string title) 
    { 
     // create a new and add it to the context 
     News item = new News { Guid = guid, Description = description, Link = link, PublishDate = publishDate, Title = title }; 
     // add the new to the context 
     db.NewsItems.InsertOnSubmit(item); 
     // save changes to the database 
     db.SubmitChanges(); 

     return item; 
    } 

Imade调试和问题是进入的消息(似乎lenght)的描述,这里是个例外:

“类型的异常“系统.InvalidOperationException'发生在 Microsoft.Phone.Data.Internal.ni.dll中,并且在 托管/本机边界之前未处理。 'System.InvalidOperationException'类型的第一次机会例外发生在 System.Data.Linq中。 ni.dll“

,这是列描述为DB

private string _description; 
    [Column] 
    public string Description 
    { 
     get 
     { 
      return _description; 
     } 
     set 
     { 
      if (_description != value) 
      { 
       NotifyPropertyChanging("Description"); 
       _description = value; 
       // Remove HTML tags. 
       _description = Regex.Replace(_description, "<[^>]+>", string.Empty); 
       // Remove newline characters 
       _description = _description.Replace("\r", "").Replace("\n", ""); 
       // Remove encoded HTML characters 
       _description = HttpUtility.HtmlDecode(_description); 
       //replace spaces 
       _description = _description.Replace(" ", ""); 

       //if (!string.IsNullOrEmpty(_description) && _description.Length > 3900) 
       // _description = _description.Substring(0, 3900); 

       NotifyPropertyChanged("Description"); 
      } 
     } 
    } 

当我去掉这一工作原理:

//if (!string.IsNullOrEmpty(_description) && _description.Length > 3900) 
// _description = _description.Substring(0, 3900); 
+2

看来问题是描述字符串的长度 – 2013-02-13 10:05:59

回答

0

感谢马里斯暗示我找到了正确的解决方案:

[Column(DbType = "ntext")] 
public string Description 

,而不是

[Column(TypeName = "varchar(MAX)")] 
public string Description 

第二个不适用于windows手机;)

+1

Ohhh,我错过了你为'windows phone'。你是对的 – Maris 2013-02-19 15:47:52

1

我们需要异常的身体,帮助你与你的问题。 但我认为(正如Emiliano Magliocca所说),问题在于您的DB中的描述单元格可以容纳较少的字符,然后尝试插入。您可以修改它将行描述的类型更改为varchar(max)或Text。无论如何提供身体的excpetion,那么我们会帮助你。

Asnwer:

你应该列描述的数据类型更改为VARCHAR(最大),那么你会觉得自由任意数量的文本保存到此collumn,因为VARCHAR(最大)最多可容纳2GB的文字。当您使用codeFirst的方式来生成表,使用这样的属性: [列(类型名=“VARCHAR(MAX)”) 公共字符串描述...

,而不是 [专栏] 公众字符串描述...

+0

谢谢,你是对的,我忘了添加异常...我增加了更多的细节。 ..如果你可以看一下;) – 2013-02-13 10:30:44

+1

你在'描述'中使用哪种类型的表格?您应该将其更改为Varchar(max),然后您可以随意将任意数量的文本保存到该列表中,因为varchar(max)最多可以容纳2GB的文本。当您使用codeFirst方法来生成表格时,请使用如下属性: [Column(TypeName =“varchar(MAX)”)] public string描述... – Maris 2013-02-13 10:41:51