来自实体框架请求的反序列化对象包含严重转义的html实体

时间:2017-01-10 22:05:21

标签: c# sql-server json entity-framework

我一直在获取一个字符串,在mssql 2012上存储为nvarchar(max),来自EF,带有严重转义的html实体。该字符串表示JSON对象。

实施例。单个&符号&将转义为&

Ex 2.转义的&符号&也会转发为&

Ex 3.撇号'被转义为'

我不知道造成这种情况的原因。数据正确存储在数据库中。我可以从SSMS访问它并查看未编码的值。如果值正确编码,那也没关系。我使用EF 5。

代码很简单:

这一行获取我想要的JSON对象 -

JsonUnpublished orgUnpubJson = putContext.JsonUnpublished.Where(j => j.OrganizationId == orgId && j.FormTypeId == JSONFORMTYPEID).SingleOrDefault();

putContext是一个从DbContext中继承并自动生成的实例。

namespace StandardOrganizationDataEntities
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class PUTDataEntities : DbContext
    {
        public PUTDataEntities()
            : base("name=PUTDataEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<FieldEditHistory> FieldEditHistory { get; set; }
        public DbSet<FormType> FormType { get; set; }
        public DbSet<FTApprovedForm> FTApprovedForm { get; set; }
        public DbSet<FTForm> FTForm { get; set; }
        public DbSet<FTMasterForm> FTMasterForm { get; set; }
        public DbSet<JsonPublished> JsonPublished { get; set; }
        public DbSet<JsonUnpublished> JsonUnpublished { get; set; }
        public DbSet<Organization> Organization { get; set; }
        public DbSet<Country> Country { get; set; }
        public DbSet<CountryDivision> CountryDivision { get; set; }
        public DbSet<FTEDoc> FTEDoc { get; set; }
        public DbSet<ProgramsAreasServedPublished> ProgramsAreasServedPublished { get; set; }
        public DbSet<ProgramsAreasServedUnpublished> ProgramsAreasServedUnpublished { get; set; }
        public DbSet<NteeCode> NteeCode { get; set; }
        public DbSet<NteeSubCode> NteeSubCode { get; set; }
    }
}

JsonUnpublished也是从DB自动生成的。

namespace StandardOrganizationDataEntities
{
    using System;
    using System.Collections.Generic;

    public partial class JsonUnpublished
    {
        public JsonUnpublished()
        {
            this.FieldEditHistory = new HashSet<FieldEditHistory>();
            this.ProgramsAreasServedUnpublished = new HashSet<ProgramsAreasServedUnpublished>();
        }

        public int JsonUnpublishedId { get; set; }
        public int OrganizationId { get; set; }
        public int FormTypeId { get; set; }
        public string JsonBlob { get; set; }
        public System.DateTime LastChangedDate { get; set; }

        public virtual ICollection<FieldEditHistory> FieldEditHistory { get; set; }
        public virtual FormType FormType { get; set; }
        public virtual ICollection<ProgramsAreasServedUnpublished> ProgramsAreasServedUnpublished { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

问题是由JSON转换为XML的奇怪情况引起的,稍后将XML转换回JSON。这种情况并非经常发生,因此难以追踪。