Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.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 mvc ASP MVC初始模型&x27;国外数据_Asp.net Mvc_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc ASP MVC初始模型&x27;国外数据

Asp.net mvc ASP MVC初始模型&x27;国外数据,asp.net-mvc,entity-framework,asp.net-mvc-4,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,我在下面的模型中有一个表位置的“位置”作为外键 public class Restaurant { public int id{ get; set; } public string name{ get; set; } public ICollection<Locations> locations { get; set; } } 当我调用location时,如何使它自动启动查询…修改您的get访问器 get { if(!_locations.Any()

我在下面的模型中有一个表位置的“位置”作为外键

 public class Restaurant
{
    public int id{ get; set; }
    public string  name{ get; set; }
    public ICollection<Locations> locations { get; set; }
}

当我调用location时,如何使它自动启动查询…

修改您的get访问器

get
{
  if(!_locations.Any()
  _locations.Add(db.Locations.First();
  return _locations;
}

你要求的是延迟加载特性。您使用什么来访问数据(我想是EF)。如果使用EF,则应启用延迟加载。但要小心,因为延迟加载可能是邪恶的特性

到目前为止,如果您希望位置与餐厅对象一起,您可以在餐厅构造函数中自动填充位置:

public class Restaurant
{
    public Restaurant() {
       // fill locations
    }

    public int id{ get; set; }
    public string  name{ get; set; }
    public ICollection<Locations> locations { get; set; }
}
公共级餐厅
{
公共餐厅(){
//填充位置
}
公共int id{get;set;}
公共字符串名称{get;set;}
公共ICollection位置{get;set;}
}
使用以下代码:

public class Restaurant
{
    public Restaurant()
    {
        locations = new List<Locations>();
    }

    public int id{ get; set; }
    public string  name{ get; set; }
    public ICollection<Locations> locations { get; set; }
}
公共级餐厅
{
公共餐厅()
{
位置=新列表();
}
公共int id{get;set;}
公共字符串名称{get;set;}
公共ICollection位置{get;set;}
}
public class Restaurant
{
    public Restaurant()
    {
        locations = new List<Locations>();
    }

    public int id{ get; set; }
    public string  name{ get; set; }
    public ICollection<Locations> locations { get; set; }
}