实体框架 - 一对多关系 [英] EntityFramework - One to many relationship
问题描述
我在MapHeader和MapDetail表
之间有一对多的关系,因此,单个地图头可以有多个mapdetails。
在数据库中,表 MapDetail 具有外键MapHeaderID,它映射到 MapHeader 表中的pk(MapHeaderId)。
I have a one-to-many relationship between MapHeader and MapDetail tables therefore there can be multiple mapdetails for a single mapheader. In the database, table MapDetail has foreign key MapHeaderID which maps to the pk(MapHeaderId) in MapHeader table.
我已经在EntityFramework Code-first中定义了它,如下所示:
I have defined it in EntityFramework Code-first as follows:
public class MapHeader
{
public int MapHeaderID { get; set; }
....
public virtual ICollection<MapDetail> mapDetails
{
get;
set;
}
}
public class MapDetail
{
public int MapDetailID { get; set; }
public int MapHeaderID { get; set; }
....
public virtual MapHeader mapheader { get; set; }
}
FLUNETAPI
modelBuilder.Entity<MapDetail>()
.HasRequired<MapHeader>(md => md.mapheader)
.WithMany(mh => mh.mapDetails)
.HasForeignKey(md => md.MapHeaderID);
它不链接! mapDetail中的mapheader属性仍然为null ...我做错了什么?
It does not link!! my mapheader property inside mapdetail record/object is still null...What am i doing wrong?
代码 - 在RAZOR VIEW中写入
foreach (MapDetail geMapDetail in Model.mapDetails)
{
if(...)
{
if(...){...}
else{
<td>
foreach(..)
{
var term = geMapDetail.Term == 0 ? geMapDetail.mapheader.Term : geMapDetail.Term;
}
</td>
}
}
代码崩溃上面的geMapDetail.mapheader,因为mapheader是null
the code crashes on the above geMapDetail.mapheader since mapheader is null
我的查询是存储过程
select distinct md.yearid, md.assessmentid, md.resulttypeid,
concat(ah.name, ' - ', a.name) as Name, md.term, md.semester, md.month, md.week,
md.MapDetailID, md.MapHeaderID, md.ColourFormatType, md.ResultTypeIDs,
md.RowOrder, md.Attendance, md.EffectSize, md.Growth, md.IndicatorID,
md.TeacherNameRequired, md.AllowEdit, md.PageHeaderID, md.IncludePreviousTerms,
md.IncludePreviousSemesters, y.year
from mapdetail md
left outer join assessments a on a.assessmentid = md.assessmentid
left outer join assessments ah on ah.assessmentID = a.headerID
left outer join years y on md.yearID = y.yearID
left outer join assessmentresulttypes art on a.assessmentid = art.assessmentid
left outer join resulttypes rt on rt.resulttypeid = art.resulttypeid
where md.mapheaderid = 22;
DBCONTEXT
public MapDetailResultSet Find_MapDetails(int mapHeaderId, int yearId, string classIds, int indicatorGroup, string indicatorIds)
{
var query = "CALL Find_MapDetails(@mapHeaderId, @yearId, @classIds, @indicatorGroup, @indicatorIds)";
MySqlParameter[] mySqlParams = new MySqlParameter[] { new MySqlParameter("mapHeaderId", mapHeaderId),
new MySqlParameter("yearId", yearId),
new MySqlParameter("classIds", classIds),
new MySqlParameter("indicatorGroup", indicatorGroup),
new MySqlParameter("indicatorIds", indicatorIds)
};
MapDetailResultSet mapdetails = new MapDetailResultSet();
using (var multiResultSet = DbContextExtensions.MultiResultSetSqlQuery(this, query, mySqlParams))
{
mapdetails.mapDetails = multiResultSet.ResultSetFor<MapDetail>().ToList();
//other result sets
...
}
return mapdetails;
}
我还在DBContext中禁用了lazyloading:(did not help)
I have also disabled lazyloading in DBContext: (didnt help)
public geContext(string connString):base(connString)
{
this.Configuration.LazyLoadingEnabled = false;
Database.SetInitializer(new MySqlInitializer());
}
更新
select distinct md.yearid, md.assessmentid, md.resulttypeid,
concat(ah.name, ' - ', a.name) as Name, md.term, md.semester, md.month, md.week,
md.MapDetailID, md.MapHeaderID, md.ColourFormatType, md.ResultTypeIDs,
md.RowOrder, md.Attendance, md.EffectSize, md.Growth, md.IndicatorID,
md.TeacherNameRequired, md.AllowEdit, md.PageHeaderID,
md.IncludePreviousTerms,
md.IncludePreviousSemesters, y.year
from mapdetail md
left outer join assessments a on a.assessmentid = md.assessmentid
left outer join assessments ah on ah.assessmentID = a.headerID
left outer join years y on md.yearID = y.yearID
left outer join assessmentresulttypes art on a.assessmentid = art.assessmentid
left outer join resulttypes rt on rt.resulttypeid = art.resulttypeid
left outer join mapheader mh on mh.mapheaderID = md.mapheaderID
where md.mapheaderid = 22;
推荐答案
不是理想的做法,无论如何,
i从存储库中加载特定的地图头,然后手动填充列表中的每个mapdetail对象。
not the ideal way to do but it got me through anyway.. i loaded the particular mapheader from the repository and then populated each mapdetail object in the list manually
控制器
MapHeaderRepository repMapHeader = new MapHeaderRepository("name=ge");
MapDetailsRepository repMapDetail = new MapDetailsRepository("name=ge");
MapDetailResultSet mapDetailResultSet = repMapDetail.FindMapDetails(mapHeaderId, yearId, classIds,
indicatorGroup, indicatorIds);
var mapHeader = repMapHeader.Get(22);
foreach (MapDetail md in mapDetailResultSet.mapDetails)
{
md.mapheader = mapHeader;
}
这篇关于实体框架 - 一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!