我在MapHeader和MapDetail表之间有一对多的关系 因此,单个mapheader可以有多个mapdetails。 在数据库中,表 MapDetail 具有外键MapHeaderID,映射到 MapHeader 表中的pk(MapHeaderId)。
我已经在EntityFramework Code-first中定义了它,如下所示:
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 ...我做错了什么?
代码 - 在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
我的查询是在存储过程中
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中禁用了延迟加载:(没有帮助)
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;
答案 0 :(得分:1)
这对我来说只适用于您的模型类。您甚至不需要FluentAPI代码,因为您的类定义遵循Code-First外键约定。我使用了这些模型类:
public class MapHeader
{
public int MapHeaderId { get; set; }
public virtual List<MapDetail> MapDetails { get; set; }
}
public class MapDetail
{
public int MapDetailId { get; set; }
public int MapHeaderId { get; set; }
public virtual MapHeader MapHeader { get; set; }
}
然后我写了一个简单的命令行程序来保存一些数据并从数据库中读回来:
private static void InsertMapHeader()
{
var header = new MapHeader
{
MapDetails = new List<MapDetail>
{
new MapDetail(),
new MapDetail(),
new MapDetail(),
new MapDetail()
}
};
using (var context = new BreakAwayContext())
{
context.MapHeaders.Add(header);
context.SaveChanges();
}
}
private static void ReadMapDetails()
{
using (var context = new BreakAwayContext())
{
var detail = context.MapDetails.FirstOrDefault();
Console.WriteLine("Header id: {0}", detail.MapHeader.MapHeaderId);
}
}
此代码获取第一个MapDetail并跟随导航属性到MapHeader。所有这些工作都没有问题,因为Code-First正在使用延迟加载来获取MapHeader导航属性。
如果您想急切加载依赖属性,可以更改查询以使用Include方法加入MapHeaders表,如下所示:
var detail = context.MapDetails.Include(d => d.MapHeader).FirstOrDefault();
您仍未显示查询代码(可能是您的Control类中的代码),因此很难说明确切的问题是什么。
答案 1 :(得分:0)
不是理想的方式,但无论如何它让我通过.. 我从存储库加载了特定的mapheader,然后手动填充列表中的每个mapdetail对象
<强>控制器强>
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;
}