实体框架,代码优先。子对象在调用时不填充 [英] Entity framework, code first. Child objects not populating when called

查看:57
本文介绍了实体框架,代码优先。子对象在调用时不填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm getting to grips with EF code first. My domain model design doesn't seem to support the auto 'populating' child of objects when I call them in code.

型号:

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; }
}

例如,我简单地称呼为:

For example, I simply call:

public List<Car> Get()
{            
    var cars = _context.Cars.ToList();
    return cars;
}

我的对象包含所有 Cars ,但不包含坐标。数据库种子可以正确创建数据,但是我无法获得EF来自动引用坐标 Client 物。但是我怀疑一旦我们解决了一个问题,就会解决另一个问题。

And my object contains all the Cars from the database, but doesn't include the Coordinates. The database seed created the data correctly, but I can't get EF to automatically reference Coordinates, or Client for that matter. But I suspect once we solve one, it'll resolve the other.

我在做什么错了,我是否误解了该怎么做?

What am I doing wrong, have I misunderstood how to do this?

推荐答案

您在这里有几个选择:


  • 要通过告诉EF到 Include()它们。例如,您可以加载 Cars ,包括其坐标客户像这样:

  • To eagerly load related entities by telling EF to Include() them. For example you can load Cars including their Coordinates and Clients like this:

public List<Car> Get()
{            
    var cars = _context.Cars
        .Include(car => car.Coordinates)
        .Include(car => car.Client)
        .ToList();
    return cars;
}


  • 通过声明导航属性<$ c来延迟加载相关的实体$ c> virtual 从而告诉EF在首次访问时加载它们。确保您没有为这样的上下文禁用延迟加载:

  • To lazy load related entities by declaring the navigation properties virtual thus telling EF to load them upon first access. Make sure you don't have disabled lazy loading for your context like this:

    this.Configuration.LazyLoadingEnabled = false;

    一个简短的示例如下:

    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!
    }
    




    • 将相关实体明确加载到上下文。然后,上下文将为您填充导航属性。看起来像这样:

      • Explicitly load related entities to the context. The context will then populate the navigation properties for you. Looks like this:

        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;
        }
        


      • 这篇关于实体框架,代码优先。子对象在调用时不填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆