为什么导航属性总是返回null,即使数据存在?

时间:2017-07-26 11:22:02

标签: entity-framework linq-to-sql asp.net-mvc-5 one-to-many

我很难坚持这个,我在两个模型之间有一对多的关系(POS_companypublic class POS_cities { [Key] public int ID { get; set; } public string Name { get; set; } public int CountryID { get; set; } public virtual POS_country country { get; set; } public virtual ICollection<POS_company> company { get; set; } }

POS_cities.cs

public class POS_company
{        
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string BusinessName { get; set; }   
    public int CityID { get; set; }
    public virtual POS_cities cities { get; set; }
}

POS_company.cs

Index

所以,我使用Entity框架来支持上面的内容,但它没有按预期生成代码,因此,我必须根据需要修改代码,例如下面的public ActionResult Index() { var pOS_company = db.POS_company.Include(p => p.cities); return View(pOS_company.ToList()); } 操作:

Include(p => p.cities)

在上面的代码中,EF没有生成Index()函数,因此,我必须明确地添加它。现在,当我执行上述cities操作时,即使数据存在于数据库中,导航属性cities也会返回null:

enter image description here

现在,让我们确保数据实际存在于数据库中:

POS_cities中的数据

enter image description here

POS_company中的数据

enter image description here

那么,可能是我做错了什么?为什么导航属性null即使数据存在于数据库中也会返回"SELECT [Extent1].[ID] AS [ID], [Extent1].[Name] AS [Name], [Extent1].[BusinessName] AS [BusinessName], [Extent1].[CityID] AS [CityID], [Extent2].[ID] AS [ID1], [Extent2].[Name] AS [Name1], [Extent2].[CountryID] AS [CountryID] FROM [dbo].[POS_company] AS [Extent1] LEFT OUTER JOIN [dbo].[POS_cities] AS [Extent2] ON [Extent1].[CityID1] = [Extent2].[ID]" ?在此先感谢:)

更新

<div class="row">
<span class="input-field col wide20">
     <input readonly="readonly" type="text" value="" class="form-control" id="<?php echo $key; ?>_name_Broker" placeholder="">
</span>
     <span class="input-field col wide20">
          <!--input type="text" class="form-control" placeholder=""-->
     <select style="width: 100%;" name="mapped_Broker" class="form-control mappedBroker" data-id="<?php echo $key; ?>" id="<?php echo $key; ?>_select_Broker">
     <option value="">--Select--</option>
         <?php foreach ($my_array_data['BrokerName'] as $data2){
             for ($i = 0; $i < count($data2['Entityname']); $i++) { ?>
             <option value="<?php echo $data2['Senetence']; ?>"><?php echo $data2['Entityname']; ?></option>
         <?php }
        }
        ?>
       </select>
<input style="display:none;" type="text" id="<?php echo $key; ?>_edit_Broker" name="Edited_Sub_Broker">
</span>
<span class="input-field col wide20">
<button type="submit" class="btn btn-dark-grey">Broker</button>
</span>
<span class="input-field col wide10">
<span class="icon-edit" id="span_edit_Broker"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" enable-background="new 0 0 64 64" id="Layer_1" version="1.1" viewBox="0 0 64 64" xml:space="preserve">
<g>
<path d="M55.736 13.636l-4.368-4.362c-0.451-0.451-1.044-0.677-1.636-0.677 -0.592 0-1.184 0.225-1.635 0.676l-3.494 3.484 7.639 7.626 3.494-3.483C56.639 15.998 56.639 14.535 55.736 13.636z"/>
<polygon points="21.922 35.396 29.562 43.023 50.607 22.017 42.967 14.39 "/>
<polygon points="20.273 37.028 18.642 46.28 27.913 44.654 "/>
<path d="M41.393 50.403H12.587V21.597h20.329l5.01-5H10.82c-1.779 0-3.234 1.455-3.234 3.234v32.339c0 1.779 1.455 3.234 3.234 3.234h32.339c1.779 0 3.234-1.455 3.234-3.234V29.049l-5 4.991V50.403z"/>
</g>
</svg> </span>
</span>

<span class="input-field col wide20">
<input readonly="readonly" type="text" class="form-control" placeholder="" 
name="brokersuggestion" value="<?php echo $jsonArrData['brokersuggestion'] ?
>">
</span>

<span class="input-field col wide10">
 <input readonly="readonly" type="text" value="" class="form-control" id="<?php echo $key; ?>_conf_Broker" placeholder="I WANT VALUE HERE"
 </span>
  </div>

1 个答案:

答案 0 :(得分:0)

试试这个:

public class POS_company
{        
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
    public string BusinessName { get; set; }   

    [ForeignKey("CityID")]
    public virtual POS_cities cities { get; set; }
    public int CityID { get; set; }

}