Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
C# 实体框架,代码优先。调用时未填充的子对象_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 实体框架,代码优先。调用时未填充的子对象

C# 实体框架,代码优先。调用时未填充的子对象,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我首先要处理EF代码。当我在代码中调用对象时,我的域模型设计似乎不支持自动“填充”对象的子对象 型号: public class Car { [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } [Required,MaxLength(10)] public string Registration { get; set; } [MaxL

我首先要处理EF代码。当我在代码中调用对象时,我的域模型设计似乎不支持自动“填充”对象的子对象

型号:

public class Car
{
    [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [Required,MaxLength(10)]
    public string Registration { get; set; }

    [MaxLength(30)]
    public string Make { get; set; }

    [MaxLength(45)]
    public string Model { get; set; }

    [Required]
    public Coordinates Coordinates { get; set; }

    [Required]
    public Client Client { get; set; }                    
}

public class Coordinates
{
    [Key, ForeignKey("Car")]
    public int Id { get; set; }

    public double Latitude { get; set; }

    public double Longitude { get; set; }

    [Required]
    public Car Car { get; set; }
}
例如,我简单地称之为:

public List<Car> Get()
{            
    var cars = _context.Cars.ToList();
    return cars;
}
公共列表Get()
{            
var cars=_context.cars.ToList();
返回车辆;
}
我的对象包含数据库中所有的
汽车
,但不包括
坐标
。数据库种子正确地创建了数据,但我无法让EF自动引用
坐标
,或者
客户端
。但我怀疑一旦我们解决了一个,它就会解决另一个


我做错了什么,我误解了怎么做了吗?

您没有
坐标的原因是因为它没有包含在查询中。有多种方法可以将其包含在结果中:

  • \u context.Cars.Include(car=>car.Coordinates.ToList()---它将在一次查询中获取带有坐标的汽车
  • 如果您不需要所有车辆的
    坐标
    ,您可以执行以下操作:将
    坐标
    属性设置为虚拟,然后当您获得车辆时,如果需要,您可以仅为其中的子集获取
    坐标
    ,并且将对每个属性的“get”访问分别调用数据库。您还将在调试器中看到EF为您创建了动态类,因此您必须将其设置为虚拟的 有关这方面的更多详细信息,请参见

  • 您在这里有两个选择:

    • 通过告诉EF急切地加载相关实体。例如,您可以加载
      汽车
      ,包括它们的
      坐标
      客户端
      ,如下所示:

      public List<Car> Get()
      {            
          var cars = _context.Cars
              .Include(car => car.Coordinates)
              .Include(car => car.Client)
              .ToList();
          return cars;
      }
      
      public class Car
      {
          // ... the other properties like in your class definition above
      
          public virtual Coordinates Coordinates { get; set;}
      }
      
      public void Get()
      {
          var cars = _context.Cars.ToList();
          var coordinates = cars.First().Coordinates; // EF loads the Coordinates of the first car NOW!
      }
      
      public List<Car> Get()
      {
          // get all cars
          var cars = _context.Cars.ToList();
      
          // get all coordinates: the context will populate the Coordinates 
          // property on the cars loaded above
          var coordinates = _context.Coordinates.ToList();
          return cars;
      }
      
      • 显式地将相关实体加载到上下文中。然后,上下文将为您填充导航属性。看起来像这样:

        public List<Car> Get()
        {            
            var cars = _context.Cars
                .Include(car => car.Coordinates)
                .Include(car => car.Client)
                .ToList();
            return cars;
        }
        
        public class Car
        {
            // ... the other properties like in your class definition above
        
            public virtual Coordinates Coordinates { get; set;}
        }
        
        public void Get()
        {
            var cars = _context.Cars.ToList();
            var coordinates = cars.First().Coordinates; // EF loads the Coordinates of the first car NOW!
        }
        
        public List<Car> Get()
        {
            // get all cars
            var cars = _context.Cars.ToList();
        
            // get all coordinates: the context will populate the Coordinates 
            // property on the cars loaded above
            var coordinates = _context.Coordinates.ToList();
            return cars;
        }
        
        公共列表Get()
        {
        //把所有的车都开走
        var cars=_context.cars.ToList();
        //获取所有坐标:上下文将填充坐标
        //上面装载的汽车上的财产
        var coordinates=_context.coordinates.ToList();
        返回车辆;
        }
        

      使用System.Data.Entity添加
      如果在
      .Include(car=>car.Coordinates)
      @sky91-使用Microsoft.EntityFrameworkCore添加
      位于类的顶部。