- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
当我使用不带外键的 JsonResult 从我的数据库 (EntityFramework) 发送一个对象时,我得到了我需要的结果。如果此对象包含 ForeignKey 关系,则该关系不在生成的 json 中:
我得到的:
{
"agentId":"9990447f-6703-11e7-9c8b-94de80ab7fee",
...
"configurationId": 22,
"configuration": null
}
我需要什么:
{
"agentId":"9990447f-6703-11e7-9c8b-94de80ab7fee",
...
"configurationId": 22,
"configuration": {
"id": 0,
...
}
}
What can i do to preserve this foreign key relation in my json?
我已经尝试设置以下内容:
services.AddMvc().AddJsonOptions(options => options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.All);
EF 上下文:
public class Agent
{
[Key]
public Guid AgentId { get; set; }
...
public int? ConfigurationId { get; set; }
[ForeignKey("ConfigurationId")]
public Configuration Configuration { get; set; }
}
public class Configuration
{
public int Id { get; set; }
...
public ICollection<Agent> Agents { get; set; }
}
Controller :
[HttpGet]
public JsonResult Index()
{
var agentList = _databaseContext.Agents;
return new JsonResult(agentList);
}
最佳答案
您可以加载 related data
[HttpGet]
public JsonResult Index()
{
var agentList = _databaseContext.Agents
.Include(agent=> agent.Configuration)
.ToList();;
return new JsonResult(agentList);
}
关于c# - ASP.NET 核心 : Preserve ForeignKey Relation in JsonResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45325006/
我正在尝试使用 limit_choices_to 来限制 Django 管理员对 ForeignKey 的选择,但我不知道如何正确地做到这一点。 如果类别 ID 为 16,此代码将执行我想要的操作,但
我有以下实体: @Entity(indices = {@Index("address")}, foreignKeys = @ForeignKey( entity = Address.
我是 Websphere 应用程序服务器的新手。请让我知道我哪里做错了。我收到 java.lang.NoSuchMethodError: javax.persistence.JoinColumn.fo
我最近将我的 Wicket 6 应用程序从 Spring 3 升级到了 Spring 4。 当我在 Jetty 7 上本地运行应用程序时,它运行良好。 当我将其部署到 Tomcat 7 时,出现以下错
这不是一个重复的问题。正如这个问题的正文中所解释的那样,出于各种原因,我不得不返回到一个旧的 Spring 和 Hibernate 项目。该项目使用这两个框架通过 Apache Tomcat 连接到
我有一个用 Spring MVC 编写并使用 Hibernate 的简单的两表应用程序。一切正常,但如果我尝试对其中一个 Controller 进行单元测试,我会收到消息: Invocation of
我正在使用这些环境开发应用程序: 打开 GlassFish 3.1.2.2 Spring 专家 MySQL hibernate POM.XML 4.0.0 com.andriasof
我正在使用 Spring Boot 在具有许多 Hibernate 依赖项的现有项目上做一个原型(prototype)。我正在尝试定义一个自定义的 LocalEntityManagerFactoryB
如何从Django自带的auth包中导入User,我需要做的: from django.contrib.auth.models import User 虽然要引用相同的 User 模型来创建 Fore
我正在尝试这样的一些: class F(models.Model): class Meta: abstract = True class C1(F): class C2(F):
感谢您检查这一点。 我的愚蠢怀疑: 我在 Django 中定义了以下模型: class School(models.Model): name = models.CharField(max_le
我有两个模型。 parent 和 child 。因为 child 和 parent 有一些不同的领域,所以我不得不将他们分开,而不是有一个单一的模型人。因为 child 应该有父亲和母亲,所以我在不同
当我尝试为以下模型进行迁移时: class Location(models.Model): name = models.CharField(max_length=200) latitu
是否可以保证ForeignKey不被删除? 最好的例子在 UserProfile 上: class UserProfile(Model): user = models.OneToOneFiel
在Django docs ForeignKey 的条目,它说: If you need to create a relationship on a model that has not yet bee
我有一个模型,我想在其中保存一个新实例(行)。我有 ForeignKey 的主键,但我没有对象本身(假设它来自某个地方)。有什么方法可以在没有原始 SQL 且无需获取实例的情况下保存它吗? 这是模型:
因此,在定义模型之前,我需要为该模型创建一个外键。考虑以下模型: class Question(models.Model): """Model for question.""" que
有一个面试测试,下面是表格和结构 Table Person = id, name, dob, dod, mother_id, father_id Primary Key (id) Foreign
前言 大家使用 Django 创建模型的时候一定会经常使用 ForeignKey 来创建两个表格之间多对一的外键关系,例如B中有一个 models.ForeignKey(A) 。而当我们需要反向查
我正在删除项目中的一些无用代码,我有机会删除自项目启动以来我们一直在使用的对第三方应用程序的依赖。我们的一个模型在第三方应用程序中有一个模型的外键,我在尝试对项目的新实例应用迁移时遇到了麻烦。 示例模
我是一名优秀的程序员,十分优秀!