验证错误:“字段Body必须是最小长度为16且最大长度为16384的字符串。”

时间:2017-03-10 04:53:30

标签: c# sql-server ef-code-first full-text-search

令我感到困惑。简单地插入大多数记录的MS SQL 2016 DB中除了大量的记录之外。没有什么明显的我可以发现这些特殊的插入物。错误信息似乎很明显,但它抱怨的事情并没有使其验证失败:

"The field Body must be a string with a minimum length of 16 and a maximum length of 16384."

表格定义:

CREATE TABLE [dbo].[FullTextSearches] (
[Id]            INT            IDENTITY (1, 1) NOT NULL,
[RecordId]      INT            NOT NULL,
[RecordType]    INT            NOT NULL,
[Title]         NVARCHAR (MAX) NULL,
[Body]          NVARCHAR (MAX) NULL,
[Accessibility] INT            NOT NULL,
CONSTRAINT [PK_dbo.FullTextSearches] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE FULLTEXT INDEX ON [dbo].[FullTextSearches]
([Title] LANGUAGE 1033, [Body] LANGUAGE 1033)
KEY INDEX [PK_dbo.FullTextSearches]
ON [FTSCatalog];

型号:

public class FullTextSearch
{
    public int Id { get; set; }
    public int RecordId { get; set; }
    public FtsRecordType RecordType { get; set; }

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

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

    public int Accessibility { get; set; }
}

代码:

        // These two lines are a hack; should not be needed
        if (body.Length < 16) body += "zzzzzzzzzzzzzzzz";
        if (body.Length > 16382) body = body.Substring(0, 16382);

            [...]

                record = new FullTextSearch
                {
                    Accessibility = accessibility,
                    Body = body,
                    RecordId = recordId,
                    RecordType = type,
                    Title = title
                };
                try
                {
                    db.FullTextSearch.Add(record);
                    db.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    Logger.LogError("FullTextSearchUtils.AddOrAmend exception (record " + recordId.ToString() + "): " + e.ToString());
                    Logger.LogError(body.Length.ToString());
                    foreach (DbEntityValidationResult res in e.EntityValidationErrors)
                    {
                        foreach (var validationError in res.ValidationErrors)
                        {
                            Logger.LogError(String.Format("(cont.) Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
                        }
                    }
                }

正在插入数据:

accessibility 0
body: "<img src=\"http://www.xxx.co.uk/images/scotland.jpg\" border=\"0\" width=\"32\" height=\"22\" /> New releases! See <a href=\"index.php?option=com_content&amp;view=article&amp;catid=3:newsflash&amp;id=69:xxx-scotland\">xxx Scotland</a>\n\r\nPeterborough meet details! See <a href=\"index.php?option=com_content&amp;view=article&amp;id=70:peterborough-meet-details\">Peterborough Meet Details</a>\n"
title: "News 17th March 2010"
recordType: 0
recordId: 71

我能想到的唯一不标准的事情是标题和正文字段的全文索引。然而,数据是一个字符串,长度在所提到的限制范围内。

现在字符串确实包含标记 - 但其他带有标记的记录已经插入正常 - 例如:

<p style="text-align: left;"><img src="images/stories/frontpic2.jpg" border="0" alt="Panel" align="right" style="margin: 10px; border: 1px;" />

(由于长度,我不会粘贴完整记录)

任何想法有什么不对?

0 个答案:

没有答案