Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 实体框架核心查询特定模型双向_Asp.net_Entity Framework_Asp.net Web Api_Asp.net Core Mvc_Entity Framework Core - Fatal编程技术网

Asp.net 实体框架核心查询特定模型双向

Asp.net 实体框架核心查询特定模型双向,asp.net,entity-framework,asp.net-web-api,asp.net-core-mvc,entity-framework-core,Asp.net,Entity Framework,Asp.net Web Api,Asp.net Core Mvc,Entity Framework Core,让我先介绍一下这个问题,我对ASP.NET Core/EF Core非常陌生 我的模型是这样的: namespace MyProject.Models { public class DeviceContext : DbContext { public DeviceContext(DbContextOptions<DeviceContext> options) : base(options) { } public DbSet<De

让我先介绍一下这个问题,我对ASP.NET Core/EF Core非常陌生

我的模型是这样的:

namespace MyProject.Models
{
    public class DeviceContext : DbContext
    {
        public DeviceContext(DbContextOptions<DeviceContext> options) : base(options) { }

        public DbSet<Device> Devices { get; set; }
        public DbSet<DeviceLocation> DeviceLocations { get; set; }

    }

    public class Device
    {
        public int Id { get; set; }
        public string DeviceName { get; set; }
        public string ServerName { get; set; }
        public string MacAddress { get; set; }
        public string LastUpdate { get; set; }
        public string WiredIPAddress { get; set; }
        public string WirelessIPAddress { get; set; }
        public DeviceLocation DeviceLocation { get; set; }
    }

    public class DeviceLocation
    {
        public int Id { get; set; }
        public string Location { get; set; }
        public virtual ICollection<Device> Devices { get; set; }
    }
}
{
    "Locations": {
        "NYC": ["ABC", "123"],
        "Boston": ["DEF", "456"],
        "Chicago": ["GHI", "789"]
    }
}
我只是很难运行第二个查询。理想情况下,输出将呈现为JSON,供API使用。我希望输出如下所示:

namespace MyProject.Models
{
    public class DeviceContext : DbContext
    {
        public DeviceContext(DbContextOptions<DeviceContext> options) : base(options) { }

        public DbSet<Device> Devices { get; set; }
        public DbSet<DeviceLocation> DeviceLocations { get; set; }

    }

    public class Device
    {
        public int Id { get; set; }
        public string DeviceName { get; set; }
        public string ServerName { get; set; }
        public string MacAddress { get; set; }
        public string LastUpdate { get; set; }
        public string WiredIPAddress { get; set; }
        public string WirelessIPAddress { get; set; }
        public DeviceLocation DeviceLocation { get; set; }
    }

    public class DeviceLocation
    {
        public int Id { get; set; }
        public string Location { get; set; }
        public virtual ICollection<Device> Devices { get; set; }
    }
}
{
    "Locations": {
        "NYC": ["ABC", "123"],
        "Boston": ["DEF", "456"],
        "Chicago": ["GHI", "789"]
    }
}
更新

如果我使用以下代码,则会出现以下错误:

代码:

错误:

  Additional information: Self referencing loop detected for property 'DeviceLocation' with type 'Project.Models.DeviceLocation'. Path 'data[0].Device[0]'.

您可以按如下所示进行尝试

注意:使用包含
的即时加载

using System.Data.Entity;

var devicesList = DeviceContext.DeviceLocations.Where(d=>d.Location = "Your-Location-Name")
                               .Include(p => p.Devices)
                               .ToList();
更新:

var devicesList = DeviceContext.DeviceLocations
                               .Include(p => p.Devices)
                               .ToList();

这将拉动单个位置,您将如何使用设备拉动所有位置?请参阅更新