我真的很难解决这个问题的措辞,以便在不看一切的情况下就有意义。我还将尝试提供尽可能多的信息,以帮助理解该问题。
首先,提供一些背景知识:我不是开发人员。我没有任何培训,只能通过做事和利用StackOverflow等出色的资源来学习。
现在,解决眼前的问题。我不确定这是否与Entity Framework Core,Asp.net Core或React相关(虽然我很确定这与React不相关,但我也将其包括在内)。
为简洁起见,我简化了模型。
我的实体模型:
public class TableA
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class TableB
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class TableC
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class TableD
{
public Guid Id { get; set; }
public Guid TableAID { get; set; }
public Guid TableBID { get; set; }
public Guid TableCID { get; set; }
public string Ticket { get; set; }
public TableA TableA { get; set; }
public TableB TableB { get; set; }
public TableC TableC { get; set; }
}
到目前为止,到目前为止,一切正常。 TableD实体用作手动链接表。
我的Web API控制器(从测试过程中可以看到,只有GET操作受此影响):
[HttpGet]
public async Task<IActionResult> GetTableDAsync()
{
var table = await _context.TableD
.Include(ta => ta.TableA)
.Include(tb => tb.TableB)
.Include(tc => tc.TableC)
.ToListAsync();
return Ok(table);
}
说我已经向我的API发布了7次,这样我的GET操作将返回一个包含4个对象的数组,这是使事情变得有趣的地方。 TableC的相关实体仅返回4次中的2次:
[
{
"id": "Guid",
"tableAID": "e16ec113-7e41-4c41-a01e-2340ad1b79c8", //this corresponds to value in tableB
"tableBID": "9ca5fa0f-663f-4788-cda1-08d688404921", //this corresponds to value in TableA
"tableCID": "97a825ff-e465-46bc-8af6-2e8f8465323e", //this corresponds to value in TableC
"tableA": {
"id": "e16ec113-7e41-4c41-a01e-2340ad1b79c8",
"name": "name"
},
"ticket": 123456,
"tableB": {
"id": "9ca5fa0f-663f-4788-cda1-08d688404921",
"firstName": "name"
},
"tableC": {
"id": "97a825ff-e465-46bc-8af6-2e8f8465323e",
"name": "name"
}
},
{
"id": "Guid",
"tableAID": "e16ec113-7e41-4c41-a01e-2340ad1b79c9", //this corresponds to value in tableB
"tableBID": "9ca5fa0f-663f-4788-cda1-08d688404922", //this corresponds to value in TableA
"tableCID": "97a825ff-e465-46bc-8af6-2e8f8465323f", //this corresponds to value in TableC
"tableA": {
"id": "e16ec113-7e41-4c41-a01e-2340ad1b79c9",
"name": "name"
},
"ticket": 123456,
"tableB": {
"id": "9ca5fa0f-663f-4788-cda1-08d688404922",
"firstName": "name"
},
"tableC": null
},
{
"id": "Guid",
"tableAID": "e16ec113-7e41-4c41-a01e-2340ad1b79c0", //this corresponds to value in tableB
"tableBID": "9ca5fa0f-663f-4788-cda1-08d688404923", //this corresponds to value in TableA
"tableCID": "97a825ff-e465-46bc-8af6-2e8f84653231", //this corresponds to value in TableC
"tableA": {
"id": "e16ec113-7e41-4c41-a01e-2340ad1b79c0",
"name": "name"
},
"ticket": 123456,
"tableB": {
"id": "9ca5fa0f-663f-4788-cda1-08d688404923",
"firstName": "name"
},
"tableC": {
"id" : "97a825ff-e465-46bc-8af6-2e8f84653231",
"name": "name"
}
},
{
"id": "Guid",
"tableAID": "e16ec113-7e41-4c41-a01e-2340ad1b79c1", //this corresponds to value in tableB
"tableBID": "9ca5fa0f-663f-4788-cda1-08d688404924", //this corresponds to value in TableA
"tableCID": "97a825ff-e465-46bc-8af6-2e8f84653232", //this corresponds to value in TableC
"tableA": {
"id": "e16ec113-7e41-4c41-a01e-2340ad1b79c1",
"name": "name"
},
"ticket": 123456,
"tableB": {
"id": "9ca5fa0f-663f-4788-cda1-08d688404924",
"firstName": "name"
},
"tableC": null
}
]
在我的SPA前端中(这不适合代码示例,而仅用于可视化),我想引起您对突出显示字段的注意。所有字段均具有应应返回值的值。 如果我运行由Entity Framework生成的原始SQL查询(来自Visual Studio中的调试),则按预期返回值。
你们中的任何人都不知道发生了什么吗,为什么随机返回我与 one 相关的实体?
如果您需要更多信息或代码,请告诉我!
编辑1: SQL表C:
dbo.TableC (
[Id] [uniqueidentifier] not null,
[Name] [string] not null)
SQL表D:
dbo.TableD (
[Id] [uniqueidentifier] not null,
[TableAID] [uniqueidentifier] not null,
[TableBID] [uniqueidentifier] not null,
[TableCID] [uniqueidentifier] not null,
[ticket] [string] not null )
答案 0 :(得分:-1)
更新:另一个建议
由于EF Core将自动修复导航属性,因此您 可能会在对象图中出现循环
您可以配置Json.NET以忽略它在对象图中找到的循环。这是通过ConfigureServices(...)
中的Startup.cs
方法完成的。
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}
选中Related data and serialization
第一个建议:确保当前请求具有新的数据库上下文
EF Core不支持在服务器上运行多个并行操作 同一上下文实例
[HttpGet]
public async Task<IActionResult> GetTableDAsync()
{
using (var_newContext = new dbContext())
{
var table = await _newContext.TableD
.Include(ta => ta.TableA)
.Include(tb => tb.TableB)
.Include(tc => tc.TableC)
.ToListAsync();
return Ok(table);
}
}
查看有关Loading Related Data和Asynchronous Queries的更多信息