相关疑难解决方法(0)

JSON.NET错误检测到类型的自引用循环

我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时

JsonConvert.SerializeObject 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误发生类型System.data.entity检测到的自引用循环.

我该如何解决这个问题?

serialization json json.net

458
推荐指数
9
解决办法
27万
查看次数

无法使用Json在Web API中序列化响应

我正在使用ASP.NET MVC 5 Web Api.我想咨询我的所有用户.

我写了api/users,我收到了这个:

"'ObjectContent`1'类型无法为内容类型'application/json; charset = utf-8'"序列化响应正文

在WebApiConfig中,我已添加以下行:

HttpConfiguration config = new HttpConfiguration();
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
Run Code Online (Sandbox Code Playgroud)

但它仍然无效.

我的返回数据函数是这样的:

public IEnumerable<User> GetAll()
{
    using (Database db = new Database())
    {
        return db.Users.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net json asp.net-web-api

102
推荐指数
9
解决办法
17万
查看次数

我应该使用实体框架4.1和MVC3启用还是禁用动态代理?

有人可以提供一些建议或指出一些可以帮助做出这个决定的博客/文章吗?代理对我来说似乎很陌生,我对使用它们犹豫不决.我喜欢通过在我的模型中使用虚拟属性来控制延迟加载的能力,但这几乎是我能看到的所有好处.我的应用程序是一个简单的MVC Web应用程序,当实体遇到更改状态时,我不需要将任何挂钩连接到上下文中.

无论如何,这是我现在非常有限的利弊列表,让我知道如果我没有任何这个.

优点

  • 在"保存"或"更新"中,我与"Apply'Changes"无缝对接
  • 延迟加载配置非常简单.

缺点

  • 从来没有为我的实体使用代理,这是一种方法的改变,对我自己和团队成员来说似乎不舒服.
  • 调试很尴尬.
  • 如果我想要序列化/反序列化,则需要额外的代码
  • 在"保存"或"更新"时,代理必须是从上下文中检索的同一对象.

orm entity-framework dynamic-proxy ef-code-first entity-framework-4.1

68
推荐指数
2
解决办法
5万
查看次数

具有EF Code First的WebApi在具有父子关系时生成错误

我对这个问题感到不满.我确实在互联网上找到了一些关于它的内容,但不是一个明确的答案.我的问题:

我必须在MVC3 Web应用程序的Model部分中的类:ParentClass和ChildClass在ParentClass上有一个属性子类型List

我使用了EF Code First,它在数据库中为我整齐地生成了父表和子表.

现在我需要一个REST服务,它返回一个List或一个ParentClass.

当我从ParentClass中删除属性Children时没有问题.但随着孩子们的到来,我不断收到错误.

错误: "The type System.Data.Entity.DynamicProxies.ParentClass_A0EBE0D1022D01EB84B81873D49DEECC60879FC4152BB115215C3EC16FB8003A was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."}

一些代码:

类别:

     public class ParentClass
{
    public int ID { get; set; }
    public string Name {get;set;}
    public virtual List<ChildrenClass> Children { get; set; }

}

public class ChildrenClass
{
    public int ID { get; set; }
    public string MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

服务:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults …
Run Code Online (Sandbox Code Playgroud)

rest entity-framework ef-code-first asp.net-mvc-3 wcf-web-api

11
推荐指数
1
解决办法
9286
查看次数

从存储过程中获取XML

我从存储过程中获取XML输出.我想要做的是获取XML并通过ASP.NET传递出来:

public XmlDocument GetPunchListXml(string communityDesc)
{
    try
    {
        using (connection = new SqlConnection(connectionString))
        {
             connection.Open();

             using (SqlCommand command = new SqlCommand("GetPunchList", connection))
             {
                   command.CommandType = CommandType.StoredProcedure;

                   SqlParameter parameter1 = new SqlParameter("@communityDesc", SqlDbType.VarChar);
                   parameter1.Value = communityDesc;
                   parameter1.Direction = ParameterDirection.Input;
                   command.Parameters.Add(parameter1);

                   var doc = new XmlDocument();
                   var reader = command.ExecuteXmlReader();
                   if (reader.Read())
                   {
                       doc.Load(reader);
                   }

                   return doc;
               }
           }
      }
      finally
      {
         connection.Close();
      }
}
Run Code Online (Sandbox Code Playgroud)

但我一直收到这些错误:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        The 'ObjectContent`1' type failed to serialize the response body for …
Run Code Online (Sandbox Code Playgroud)

c# xml sql-server asp.net

8
推荐指数
1
解决办法
1807
查看次数

是否以JSON格式从LINQ到SQL检索数据?

我将数据从数据库序列化为JSON格式时遇到问题。我正在使用WebAPI 2和Entity Framework6。我已经用EF创建了一个模型。甚至创建具有内容的数据库和表。当我使用下面的代码时,键入http:// localhost:54149 / api / qr_group时出现错误

控制器:

private EfContext db = new EfContext();

// GET api/<controller>
public IEnumerable<QR_Group> GetGroups()
{
    var result = from g in db.QR_Groups
                 select g;
    return result.ToList();
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用Newtonsoft.Json将表与JSON格式的内容序列化。

我尝试了以下代码而不是上面的代码:

public IQueryable<QR_Group> GetGroups()
{
  var groupList = (from g in db.QR_Groups
                   select new
                   {
                     name = g.name,
                     code = g.code
                   }).AsQueryable();

  var json = JsonConvert.SerializeObject(groupList);

  return json; //error CS0029: Cannot implicitly convert type `string' to `System.Linq.IQueryable<RestApi.Models.QR_Group>'
}
Run Code Online (Sandbox Code Playgroud)

我收到错误CS0029。如何解决此问题以返回json中的数据?提醒一下:QR_Group实体具有3列(ID,名称,代码)

c# linq json entity-framework asp.net-web-api

3
推荐指数
1
解决办法
8143
查看次数