Asp.net mvc ASP.NETMVC2中的关联
我在使用VS2010的web应用程序项目中使用LINQ作为ORM。我在SQL数据库中定义了ER,并简单地将数据库表拖放到ORM中。传输表与具有4个不同列的医院表有4个关系。其中一列是FK,int,而不是null。其他三个是FK、int和null。在DBML中,Transfer类与Hospital类有4个关联。与所需Transfer.Hospital(FK,int,非null)的关联具有以下属性Asp.net mvc ASP.NETMVC2中的关联,asp.net-mvc,Asp.net Mvc,我在使用VS2010的web应用程序项目中使用LINQ作为ORM。我在SQL数据库中定义了ER,并简单地将数据库表拖放到ORM中。传输表与具有4个不同列的医院表有4个关系。其中一列是FK,int,而不是null。其他三个是FK、int和null。在DBML中,Transfer类与Hospital类有4个关联。与所需Transfer.Hospital(FK,int,非null)的关联具有以下属性 Cardinality: OneToMany Child Property: True Acces
Cardinality: OneToMany
Child Property: True
Access: Public
Inheritance Modifier: (None)
Name = Transfers1
Parent Property
Access: Public
Inheritance Modifier: (None)
Name: HospitalSrcOrDest
Participating Properties: Hospital.HospitalID -> Transfer.Hospital
Unique: False
视图页面使用的视图模型具有传输类型的传输属性。Model.Transfer.Hospital始终具有整数值。但是,以下页面脚本中的Model.Transfer.HospitalSrcOrDest始终为空。HospitalName不应该通过Hospital_Transfer1关联自动检索吗?谢谢
<%: Model.Transfer.HospitalSrcOrDest != null ? Model.Transfer.HospitalSrcOrDest.HospitalName : string.Empty%>
这可能是一个延迟加载问题,您是否在视图访问医院之前显示datacontext?默认情况下,L2S延迟加载关系。这意味着在实际访问属性之前,它不会从数据库中获取它。如果视图访问医院时您的连接已断开,它可能无法检索。只是猜测而已 要进行测试,请在控制器中执行加载/选择后,立即尝试访问医院以强制加载 如果你想一直渴望去医院,这里有一个链接,告诉你该怎么做:
我修改了我的存储库构造函数,但没有任何区别
public AppRepository() //constructor
{
// Create DataLoadOptions
DataLoadOptions dlo = new DataLoadOptions();
// Always fetch source or destiation hospital when we get transfer
dlo.LoadWith<Transfer>(t => t.HospitalSrcOrDest);
// Set these options on the DataContext
db.LoadOptions = dlo;
....
}
我按照建议在存储库构造函数中设置了loadoption,但没有任何区别。//创建DataLoadOptions DataLoadOptions dlo=new DataLoadOptions();//当我们获得帖子dlo.LoadWith(p=>p.tags)时总是获取标签;//在DataContext db.LoadOptions=dlo上设置这些选项;
public ActionResult Edit(string id)
{
....
Transfer transfer = base.ApplicationRepository.GetTransfer(intID);
if (transfer == null)
{
ViewData["Message"] = "There is no transfer record";
return View("NotFound");
}
TransferViewModel vm = new TransferViewModel(base.ApplicationRepository, transfer, "Edit");
return View(vm);
}