gpt4 book ai didi

json - Entity Framework 数据库首先使用 Web API

转载 作者:行者123 更新时间:2023-12-02 04:56:22 26 4
gpt4 key购买 nike

是否可以同时使用 Entity Framework 和 Web API?

应用结构:

  1. 使用 AngularJS 的 Web API 应用程序
  2. 数据访问层:使用 Entity Framework

在我的 Web API 应用程序中,我想使用 DAL 中的实体作为模型。

从 Web API Controller 返回的数据应该是 JSON。

当我尝试时,总是会出现这样的错误:

{"$id":"1","Message":"发生错误。","ExceptionMessage":"'ObjectContent1' 类型无法序列化内容类型 'application' 的响应正文/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"$id":"2","Message":"一个错误有发生。”,“ExceptionMessage”:“将内容复制到流时出错。”,“ExceptionType”:“System.Net.Http.HttpRequestException”,“StackTrace”:null,“InnerException”:{“$id”:“3","Message":"发生错误。","ExceptionMessage":"ObjectContext 实例已被释放,不能再用于需要连接的操作。","ExceptionType":"System.ObjectDisposedException"“StackTrace”:“在 System.Data.Objects.ObjectContext.EnsureConnection()\r\n 在 System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption)\r\n 在System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator()\r\n 在 System.Collection s.Generic.List1..ctor(IEnumerable1 集合)\r\n 在 System.Linq.Enumerable.ToList[TSource](IEnumerable`1 源)\r\n 在 Newtonsoft.Json .Serialization.JsonArrayContract.CreateWrapper(对象列表)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter 编写器,对象值,JsonContract valueContract,JsonProperty 成员,JsonContainerContract containerContract,JsonProperty containerProperty)\r\n 在 Newtonsoft。 Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,对象值)\r\n 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值)\r\n 在 Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,对象值)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c_DisplayClassd.b_c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(操作 Action , CancellationToken 代币)"}}}

这是我的 Controller (当我使用 Code First 时相同的代码在工作)

public class TodoController : ApiController
{
// GET api/Todo
public IEnumerable<Todo> GetTodoItems(string q = null, string sort = null,
bool desc = false, int? limit = null, int offset = 0)
{
using (var db = new AdvanceContext())
{
var list = ((IObjectContextAdapter)db).ObjectContext.CreateObjectSet<Todo>();

IQueryable<Todo> items = string.IsNullOrEmpty(sort)
? list.OrderBy(o => o.Priority)
: list.OrderBy(String.Format("it.{0} {1}", sort, desc ? "DESC" : "ASC"));

if (!string.IsNullOrEmpty(q) && q != "undefined")
items = items.Where(t => t.Description.Contains(q));

if (offset > 0)
items = items.Skip(offset);

if (limit.HasValue)
items = items.Take(limit.Value);

return items;
}
}
}

最佳答案

在您的上下文中添加以下行:

this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;

示例:

public partial class Dbname : DbContext
{
public Dbname()
: base("name=Dbname")
{
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = false;
}

public virtual DbSet<dtproperty> dtproperties { get; set; }

关于json - Entity Framework 数据库首先使用 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com