声明;我是一个mvc nub。 我有点卡住了,就像几天前一样,我不知道为什么现在不行。问题是我没有得到子类数据,只有“普通”属性的数据。我在Global.asax中填充了5行,而表RedayModels在DB中。数据库中的键也可以。
编辑:两个表ReWeekModel和ReDayModel都有数据。我正在使用代码优先方法。
我有这个DB:
public class ReChronoDB : DbContext
{
public DbSet<ReWeekRowModel> WeekRows { get; set; }
}
这是我的DbSet行
public class ReWeekRowModel
{
public ReWeekRowModel()
{
CreatedDate = DateTime.Now;
Days = new List<ReDayModel>()
{
new ReDayModel {DayName = DayOfWeek.Monday},
new ReDayModel {DayName = DayOfWeek.Tuesday},
new ReDayModel {DayName = DayOfWeek.Wednesday},
new ReDayModel {DayName = DayOfWeek.Thursday},
new ReDayModel {DayName = DayOfWeek.Friday},
new ReDayModel {DayName = DayOfWeek.Saturday},
new ReDayModel {DayName = DayOfWeek.Sunday}
};
}
public DateTime CreatedDate { get; private set; }
[Key]
public int Id { get; set; }
public string Name { get; set; }
[DisplayName("Week #"), Range(1, 53)]
public int WeekOfYear { get; set; }
[Range(2017,2075)]
public int Year { get; set; }
public List<ReDayModel> Days { get; set; }
public Priority Priority { get; set; }
public Status Status { get; set; }
public Category Category { get; set; }
public Responsible Responsible { get; set; }
public ResponseGroup ResponseGroup { get; set; }
public string CaseRef { get; set; }
[StringLength(75), DisplayName("Short Description")]
public string Description { get; set; }
public string Details { get; set; }
}
这里是子类:
public class ReDayModel
{
[Key]
public int Id { get; set; }
public DayOfWeek DayName { get; set; }
public double Hours { get; set; }
public string Comments { get; set; }
}
最后,这是带有Index Action的控制器。
public class ReChronoController : Controller
{
ReChronoDB _reChronoDb = new ReChronoDB();
// GET: ReChrono
public ActionResult Index(string responsegroup = "", string responsible = "", int week = 0, int year = 0)
{
CultureInfo ciCurr = CultureInfo.CurrentCulture;
ViewBag.CurrentWeek = ciCurr.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek,
DayOfWeek.Monday);
ViewBag.CurrentYear = ciCurr.Calendar.GetYear(DateTime.Today);
var model = _reChronoDb.WeekRows.ToList();
// For dropdownlists
ViewBag.Years = _reChronoDb.WeekRows.Select(r => r.Year).Distinct();
ViewBag.Weeks = _reChronoDb.WeekRows.Select(r => r.WeekOfYear).Distinct();
var filteredmodel = ReChronoDomainLayer.FilteredWeekRows(model, responsegroup, responsible, week, year);
return View(filteredmodel);
}
}
当我将鼠标悬停在“model”变量上并向下钻取时,行的Days集合没有值。
编辑:这是正在执行的sql:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[CreatedDate] AS [CreatedDate],
[Extent1].[Name] AS [Name],
[Extent1].[WeekOfYear] AS [WeekOfYear],
[Extent1].[Year] AS [Year],
[Extent1].[Priority] AS [Priority],
[Extent1].[Status] AS [Status],
[Extent1].[Category] AS [Category],
[Extent1].[Responsible] AS [Responsible],
[Extent1].[ResponseGroup] AS [ResponseGroup],
[Extent1].[CaseRef] AS [CaseRef],
[Extent1].[Description] AS [Description],
[Extent1].[Details] AS [Details]
FROM [dbo].[ReWeekRowModels] AS [Extent1]
编辑:这是上下文?
public class ReChronoDBInitializer :
DropCreateDatabaseIfModelChanges<ReChronoDB>
{
protected override void Seed(ReChronoDB context)
{
base.Seed(context);
var enumToLookup = new EnumToLookup();
enumToLookup.Apply(context);
ReChronoDomainLayer.CreateDummyEntries(context);
}
}
似乎完全忽略了List Days集合。
我也尝试过没有过滤模型,但我把它放在这里,所以你明白为什么我在动作中有参数。
有什么想法吗?
答案 0 :(得分:0)
首先请确保ReDayModel
也是ReChronoDB
的一部分:
public class ReChronoDB : DbContext
{
public DbSet<ReWeekRowModel> WeekRows { get; set; }
public DbSet<ReDayModel> DayRows { get; set; }
}
现在您的控制器操作尝试使用延迟加载,但由于Days
属性的属性类型不正确而被禁用。如果要启用延迟加载,您的类应如下所示:
public class ReWeekRowModel
{
...
public virtual ICollection<ReDayModel> Days { get; set; }
...
}
作为替代方案,您可以使用急切加载,这需要更改您的操作才能使用.Include()
方法:
public class ReChronoController : Controller
{
...
public ActionResult Index(string responsegroup = "", string responsible = "", int week = 0, int year = 0)
{
...
var model = _reChronoDb.WeekRows.Include(w => w.Days).ToList();
...
}
}
如果您始终需要Days
集合,则可能会带来更好的性能。请阅读更多here。