我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时
JsonConvert.SerializeObject
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误发生类型System.data.entity检测到的自引用循环.
我该如何解决这个问题?
我正在使用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) 有人可以提供一些建议或指出一些可以帮助做出这个决定的博客/文章吗?代理对我来说似乎很陌生,我对使用它们犹豫不决.我喜欢通过在我的模型中使用虚拟属性来控制延迟加载的能力,但这几乎是我能看到的所有好处.我的应用程序是一个简单的MVC Web应用程序,当实体遇到更改状态时,我不需要将任何挂钩连接到上下文中.
无论如何,这是我现在非常有限的利弊列表,让我知道如果我没有任何这个.
优点
缺点
orm entity-framework dynamic-proxy ef-code-first entity-framework-4.1
我对这个问题感到不满.我确实在互联网上找到了一些关于它的内容,但不是一个明确的答案.我的问题:
我必须在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
我从存储过程中获取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) 我将数据从数据库序列化为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# ×3
json ×3
asp.net ×2
json.net ×1
linq ×1
orm ×1
rest ×1
sql-server ×1
wcf-web-api ×1
xml ×1