[英]Setup query, navigation properties (Code First) in view (Entity Framework 6 + MVC 5)
我想要实现的目标非常简单。 首先,通过代码,我希望允许用户进行一系列的联系。 他们每个人都有以下选项:“无”,“还没有遇到”和“客户”。 我正在寻找的是将选择的选项添加到数据库中并检索其值。
问题是我不太清楚如何正确建立关系。 或者我不知道我正在执行的查询是否实际上是在管理关系。
我要采用的方法很简单:我创建一个下拉列表(基于字典),该列表从“ ExposureResult”表(该表是我之前提到的选项所在的表)中检索数据:无,尚未遇到,客户端) ,并在“联系人”创建表单中将其显示给用户。
提交后,它将为我提供“ ExposureResult”表中的行的ID,并将其插入ExposureResultID(这是我认为已设置为关系的属性),因此以后可以调用它。
稍后我执行以下查询:
var query = from c in dbs.ContactsDB where c.IboID == uid select c;
return query;
其中dbs.ContactsDB是我已实现的Contacts模型的数据库上下文,并检索了我要查找的所有值。
查询变量将以IEnumerable返回给我模型。
然后,在.cshtml文件中,我将这样称呼它:
@foreach(模型中的变量项){
item.ExposureResult}
但这不会返回我期望的结果,即得到ID值在ExposureResult中引用的内容,而不是ID本身。
这是模特
该模型如下:
public class Contacts
{
[HiddenInput(DisplayValue=false)]
[Key]
public int ContactsID { get; set; }
[HiddenInput(DisplayValue=false)]
public int IboID { get; set; }
public string Name { get; set; }
[Display(Name="First Time Met")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public string FirstMeet { get; set; }
public string Phone { get; set; }
[Display(Name="Date of Contact")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? DateOfContact { get; set; }
public string Comments{ get; set; }
[Display(Name="Next Exposure")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? NextExposure { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Next Exposure 2")]
public DateTime? NextExposure2 { get; set; }
//This is analogue to SelectedQ from Ibo Model
//The main purpose is to receive the incoming data from the SelectList
[NotMapped]
public string SelectedResult { set; get; }
[Display(Name = "Result")]
public int ExposureResultID { get; set; }
public virtual ICollection<ExposureResults> ExposureResult {get; set;}
//public virtual ICollection<ExposureResults> ExposureResults { get; set; }
public virtual ICollection<Ibo> Ibo { get; set; }
}
public class ExposureResults
{
public int ID { get; set; }
public string ExposureResult { get; set; }
}
我正在制作一张图片,以更好地解释我要实现的目标:
如果我真正了解您的意思,则需要在查询中包括“ ExposureResult”属性,并向ExposureResults类添加外键属性。
//include
dbs.ContactsDB.Include("ExposureResult") or dbs.ContactsDB.Include(er => er.ExposureResult)
public class ExposureResults {
public int ID { get; set; }
public string ExposureResult { get; set; }
//foreign key property
public int ContactsID
}
有关更多信息,请参见本文 。
希望对您有所帮助!
我设法做到了。 我还在ASP.NET论坛中发布了此问题和答案:
http://forums.asp.net/p/2014225/5798579.aspx?p=True&t=635498275668626346&pagenum=1
它去了:
首先,我将创建一个int属性,无论我想要什么名称。 在这种情况下,为了保持一致性,我决定创建以下属性:
public int ExposureResultID { get; set; }
此属性将是外键,它将具有ExposureResult表/模型的引用行。 但这还不够。 实体框架不会神奇地知道这实际上是外键。
因此,我们然后添加以下属性:
[ForeignKey("ExposureResultID")]
public virtual ExposureResult ExposureResult { get; set; }
如您所见,我添加了“ ExposureResult”类型,该类型引用了表/模型/类,该表/模型/类具有要引用的数据(外部表)。
请注意,我已在“虚拟”关键字下划线并加粗。 必须说出来。 这将允许延迟加载工作。
然后,如您所见,它具有“数据注释”属性“外键”。 在这里,您可以告诉Entity Framework谁是外键。 就我而言,我声明的属性为:ExposureResultID:
然后,将模型传递给视图,并运行一个foreach循环:
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.ExposureResult.ExposureResults)
}
这将使延迟加载工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.